摘要: 今天看了一下《算法导论》的KMP算法原理,以前没搞明白的地方终于搞懂了,其核心主要是计算前缀函数实现代码如下,visual studio 2005编译通过,只简单的测试了几个字符串,也不知道写的对不对 1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 bool kmpmatch(string s, string p, int *a) 6 { 7 int q = 0 ; 8 for( int i = 0; i <= s.size(); i++) 9 {10 if( q... 阅读全文
posted @ 2011-10-07 16:56 可可犀利 阅读(165) 评论(0) 推荐(0)
摘要: 1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 void sumofsub( int *a, int k, int sum , int M, int r,vector<int> element) 6 { 7 if( sum + a[k] == M ) 8 { 9 element.push_back(a[k]);10 for(int i = 0 ; i < element.size() ; i++)11 {12 co... 阅读全文
posted @ 2011-10-06 20:51 可可犀利 阅读(189) 评论(0) 推荐(0)
摘要: 参考了http://www.cnblogs.com/phinecos/archive/2009/09/11/1564975.html之后,写的大整数N进制转M进制算法 1 #include <vector> 2 #include <iostream> 3 4 using namespace std; 5 6 char IntToChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8 阅读全文
posted @ 2011-10-04 12:43 可可犀利 阅读(181) 评论(0) 推荐(0)
摘要: 双队列实现栈的主要思路,是一个队列为主队列,另外一个为辅助队列。每次从栈中pop出一个数,实质就是每次取队尾元素,假设主队列中有N个元素,那么就将前N-1个元素放入辅助队列当中,此时主队列当中只有队尾元素,将其取出,然后再将辅助队列当中的元素全部再放回主队列当中,一次pop动作完成。栈的push动作实现,就是将元素push进主元素就可以了实现代码和测试结果如下: 1 #include <queue> 2 #include <iostream> 3 using namespace std; 4 5 class exQueue 6 { 7 public: 8 exQueue 阅读全文
posted @ 2011-10-02 22:40 可可犀利 阅读(312) 评论(0) 推荐(0)
摘要: #include <iostream>#include <string>using namespace std;int StrToNum(const string s){ //if( s == 0) return 0; bool flagminus = false; if(s[0] == '-') flagminus = true; int num = 0 ; if(!flagminus) { for(int i = 0; i <= s.size() - 1; i++) { num = num * 1... 阅读全文
posted @ 2011-10-01 10:48 可可犀利 阅读(156) 评论(0) 推荐(0)
摘要: #include <iostream>using namespace std;enum {COLUMN,ROW};int _tmain(int argc, _TCHAR* argv[]){ int flag ; cout << "please input the number N" << endl; int n; cin >> n; int **p = new int*[ n ]; for(int i = 0 ; i < n ; i++) { p[i] = new int [n]; } for( int i = 0 ;. 阅读全文
posted @ 2011-10-01 10:45 可可犀利 阅读(214) 评论(0) 推荐(0)