void Make_Next (char *str) {
int len = strlen (str + 1);
for (int i = 2, j = 0; i <= len; i++) {
while (j != 0 && str[j + 1] != str[i]) j = next[j];
if (str[j + 1] == str[i]) {
j++;
}
next[i] = j;
}
}
int KMP (char *sa, char *sb) {
int lena = strlen (sa + 1), lenb = strlen (sb + 1);
for (int i = 1, j = 0; i <= lena && j <= lenb; i++) {
while (j != 0 && sb[j + 1] != sa[i]) j = next[j];
if (sb[j + 1] == sa[i]) {
j++;
}
if (j == lenb) {
return i - j + 2;
}
}
return -1;
}
12-13
1606
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-21
350
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交