KMP算法C++实现
2014-06-06 20:13
246 查看
#include <iostream> #include <cstring> using namespace std; int *getNext(const char *T,int len) { int i = 0; int j = -1; int *next = new int[len]; next[0] = -1; while(i < len - 1) { if(j == -1 || T[i] == T[j]) { i++; j++; next[i] = j; } else { j = next[j]; } } return next; } int KMP(const char *S,const char *T) { int i = 0; int j = 0; int lens = strlen(S); int lent = strlen(T); int *next = getNext(T,lent); while(i < lens && j < lent) { if(j == -1 || S[i] == T[j]) { i++; j++; } else { j = next[j]; } } delete[] next; if(j == lent) return i - lent; return -1; } int main() { char S[100],T[100]; cin >> S >> T; cout << KMP(S,T) + 1 << endl; return 0; }
相关文章推荐
- KMP算法的C++实现代码
- 数据结构_串_串的模式匹配_KMP算法_C++实现
- KMP算法C++实现
- KMP算法——C++优化实现
- KMP算法匹配原理以及C++实现
- KMP算法C++实现
- KMP算法(C++实现)
- KMP算法C++ 实现
- kmp算法c++代码实现
- KMP算法及c++实现
- 数据结构_串_用链表做存储结构实现KMP算法_C++实现
- 算法串匹配C++实现字符串匹配的KMP算法
- C++实现字符串匹配的KMP算法
- KMP算法 C++实现
- kmp算法C++源码实现
- KMP算法 (C++实现)
- C++实现的KMP算法
- KMP算法 (C++实现)
- [C++]KMP算法实现
- 逐步理解KMP算法C++完整实现