poj3461Oulipo

来源:互联网 发布:windows route delete 编辑:程序博客网 时间:2024/06/04 23:21
</pre><pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N=10005;const int MAXN=1000005;/*void _next(const char *s,int *next){//    以数组的下标表示已经匹配的字符数 ,以下标对应的数据存储 真后缀(自身最长前缀)的长度    int i,j,n=strlen(s);    next[0]=-1;//    for(j=-1,i=0;i<n;++i){        while(j!=-1&&s[i]!=s[j]){            j=next[j];        }        next[i+1]=++j;    }}*/void _next(const char *s,int *next){//    以数组的下标表示已经匹配的字符数 ,以下标对应的数据存储 真后缀(自身最长前缀)的长度    next[0]=-1;//    int j=-1;    for(int i=1;s[i];++i){        while(j!=-1&&s[i]!=s[j+1]){            j=next[j];        }        if(s[i]==s[j+1])j++;        next[i]=j;    }}int kmp(char *a,char *b,int *next){    int i,j=-1,ret=0;    for(i=0;a[i];i++){        while(j!=-1&&a[i]!=b[j+1])j=next[j];            if(a[i]==b[j+1])j++;            if(!b[j+1]){                ret++;                j=next[j];            }    }    return ret;}int main(){    char w[N],t[MAXN];    int T,next[N];    bool first=true;    cin>>T;    while(T--){        memset(next,0,sizeof(next));        if(first){            getchar();            first=false;        }        gets(w);        gets(t);        _next(w,next);        //for(int i=0;i<strlen(w);i++)cout<<next[i]<<endl;        printf("%d\n",kmp(t,w,next));    }    return 0;}


0 0