KMP

来源:互联网 发布:睡衣推荐 知乎 编辑:程序博客网 时间:2024/06/15 16:42
const int maxn = 10000 + 5;char s[maxn];char t[111];//在s中匹配tint __next[111];void GetNext() {    __next[0] = -1;    int k = -1, j = 0;    for (; t[j] != '\0'; ) {        if (k == -1 || t[j] == t[k]) { //如果匹配            j++;            k++;            __next[j] = k;        }        else             k = __next[k]; //如果不匹配    }}int kmp() {    int j = 0, i = 0;    for (; s[i] != '\0'; ) {        if (t[j] == '\0') return (i - j); // 返回数组下标起始位置        if (j == -1 || s[i] == t[j]) {            i++;            j++;        }        else             j = __next[j];    }    return -1;}
0 0
原创粉丝点击