poj 2752 Seek the Name, Seek the Fame(kmp)

来源:互联网 发布:手机淘宝如何切换账号 编辑:程序博客网 时间:2024/05/01 07:51

求相同前后缀的位数……

代码在这:

#include <bits/stdc++.h>using namespace std;const int N=400010;char s[N];int x;int next[N],res[N];void fnext(){    int i,k;    int n=strlen(s);    next[0]=-1;    for(i=1,k=-1;i<n;++i)    {        while(k!=-1&&s[i]!=s[k+1]) k=next[k];        if(s[i]==s[k+1])            k++;        next[i]=k;    }    x=i;}int main(){    int i;    while(scanf("%s",s)!=EOF)    {        fnext();        i=x-1;        int num=0;        while(i!=-1)        {            res[++num]=i+1;            i=next[i];        }        for(int i=num;i>1;i--)            printf("%d ",res[i]);        printf("%d\n",res[1]);    }    return 0;}


0 0
原创粉丝点击