KMP中next数组实现
#include <iostream>
using namespace std;
int main()
{
char a[] = {'a','b','c','d','a','b','d'};
int next[7];
next[0] = 0;
int j = 0;
int i = 1;
while( i<7 ){
if(a[next[i-1]] == a[i]){
next[i] = next[i-1] +1;
}else if(a[0] == a[i]){
next[i] = 1;
}else{
next[i] = 0;
}
i++;
}
while( j < 7){
cout << next[j++] << endl;
}
return 0;
}
本文深入探讨了KMP算法中的next数组实现过程,通过一个具体的字符串匹配例子,详细展示了如何构建next数组,以及next数组在提高模式串匹配效率方面的作用。
8226

被折叠的 条评论
为什么被折叠?



