kmp模板

来源:互联网 发布:php curl get请求参数 编辑:程序博客网 时间:2024/05/21 11:14
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN=100000;char a[MAXN],b[MAXN];int i,j,n,m,next[MAXN],p;int main(){    scanf("%s",&a[1]);    scanf("%s",&b[1]);    m=strlen(&b[1]); n=strlen(&a[1]);    next[1]=0; j=0;    for (i=2;i<=m;i++)    {        while ((j>0)&&(b[i]!=b[j+1])) j=next[j];        if (b[i]==b[j+1]) j++;        next[i]=j;    }    j=0; p=-1;    for (i=0;i<=n;i++)    {        while ((j>0)&&(a[i]!=b[j+1])) j=next[j];        if (a[i]==b[j+1]) j++;        if (j==m)        {            if (p==-1) { printf("Ok!\n"); p=1; }            printf("The %dth Answer is %d\n",p,i-m+1); p++;            j=next[j];        }    }     if (p==-1) printf("Sorry!Not found!\n");    return 0;}
自己打的一个kmp模板,有错误请指出~~
0 0
原创粉丝点击