BF:
int BFstr(char *s,char *t){
int i = 0,j = 0,k = -1;
int lens = strlen(s);
int lent = strlen(t);
while(i < lens && j < lent){
if(s[i] == t[j]){
i++;
j++;
}
else{
i = i - j + 1;
j = 0;
}
}
if(j == lent)
k = i - j;
return k;
}
KMP:
1、原数组不回溯,匹配数组减少倒回次数。若前缀和后缀相同,可直接倒回前缀的末尾,在原数组原来位置继续比较;
2、求next[ ]数组。
(图片next[ ]数值与我的不一样,只是示例想法)
我的next[ i ] = 子串[ 0 -> i ]字符的最长相等前后缀的前缀的最后一位的下标;
求解next数组:对于代求字符串 s[ ] , 进行到s[ i ] 时,若 s[ i ] == s[ next[i - 1] + 1],即可在s[0~i-1]的最长相等前后缀扩展一个s[ i ]。
若 s[ i ] != s[ next[i - 1] + 1],则应寻找第二长的s[0~i-1]的相等前后缀,看看能不能使其相等,而注意,s[0~ j ](这里 j 总为next[ i - 1],即 j 总指向s[0~i-1]的最长相等前后缀的前缀的最后一位的下标)与s[0~i-1]的后缀是相等的,寻找其第二长的相等前后缀,即寻找s[0~ j ]的最长相等前后缀,则直接令 j = next[ j ]。
void getnext(char s[],int len){
int j = -1;
next[0] = -1;
for(int i = 1;i < len;i++){
while(j != -1 && s[i] != s[j+1]){
j = next[j];
}
if(s[i] == s[j+1]){
j++;
}
next[i] = j;
}
}
总代码
void Get_next(char*t,int next[]){
int j,k,lent;
lent = strlen(t);
next[0] = -1;
next [1] = 0;
j = 1,k = 0;
while(j < lent){
if(t[k] == t[j]){
k++,j++;
next[j] = k;
}else if(k == 0){
j++;
next[j] = 0;
}else{
k = next[k];
}
}
}
int KMPstr(char*s,char*t,int next[]){
int i = 0,j = 0,k = -1;
int lens = strlen(s),lent = strlen(t);
while(i < lens && j < lent){
if(s[i] == t[j]){
i++,j++;
}else if(j == 0){
i++;
}else{
j = next[j];
}
}
if(j == lent)
k = i - lent;
return k;
}