- kmp解决了求所有主串的前缀pre[i] (0 <= i < n)的后缀与模式串前缀的最大匹配长度问题
- 拓展kmp解决了所有主串的后缀suf[i]的前缀,与模式串前缀的最大匹配长度问题.
- 当且仅当len%(len-next[len])==0时,str[next[len]~len-1]为最小循环节
//KMP
int kmp(char str[],char sub[]){
int i,j,counts = 0;
int lenstr = strlen(str);
int lensub = strlen(sub);
int next[lensub+1];
memset(next,0,sizeof(next));
for(i = 1; i < lensub; i++){
j = next[i-1];
while(sub[i] != sub[j] && j > 0)
j = next[j-1];
if(sub[i] == sub[j]){
next[i] = j + 1;
if(j > 0)//匹配时的优化,单纯求next数组没有这条语句
next[i-1] = next[j-1];
}
}
i = 0; j = 0;
while(i < lenstr){
if(str[i] == sub[j]){
i++; j++;
}
else{
if(j == 0)
i++;
else
j = next[j-1];
}
if(j == lensub){
counts++; j = next[j-1];
}
}
return counts;
}
const int MAXN = 200010;
int nexts[MAXN], extend[MAXN];
void getNext(char T[]){
int length = strlen(T);
int i,a,p, j = -1;
nexts[0] = length;
for(i = 1; i < length; ++i,--j){
if(j<0 || i+nexts[i-a] >= p){
if(j < 0)
j = 0, p = i;
while(p<length && T[p]==T[j])
++j, ++p;
nexts[i] = j, a = i;
}
else nexts[i] = nexts[i-a];
}
}
void getExtend(char S[], char T[]){
memset(nexts,0,sizeof(nexts));
getNext(T);
int Slen = strlen(S), Tlen = strlen(T);
int i,a,p, j = -1;
for(i = 0; i < Slen; ++i,--j){
if(j<0 || i+nexts[i-a] >= p ){
if(j < 0)
j = 0, p = i;
while(p<Slen && j<Tlen && S[p]==T[j])
++j, ++p;
extend[i] = j; a = i;
}
else extend[i] = nexts[i-a];
}
}