POJ 2752 Seek the Name, Seek the Fame

来源:互联网 发布:任我行 联通网络 编辑:程序博客网 时间:2024/04/28 05:43

KMP第二题,如果对KMP理解了的话,这道题应该想一下就能想出来

#include<stdio.h>#include<algorithm>using namespace std;char s[400005];int next[400005];int a[400005];//next数组范围为1-n,next[0]是不使用的void get_next(int n){    if(n==0) return;    int i,j;    j=next[0]=-1;    i=0;    while(i<n){        while(j!=-1 && s[j]!=s[i]) j=next[j];        next[++i]=++j;    }    int len=0;    for(int i=next[n];i!=0;i=next[i]) a[len++]=i;    for(int i=len-1;i>=0;i--) {        printf("%d ",a[i]);    }    //if(next[n]!=0) printf("%d ",next[n]);}int main(){    #ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);    #endif // ONLINE_JUDGE    int cas=0;    while(scanf("%s",s)!=EOF){        int len=0;        while(s[len]!='\0') len++;        get_next(len);        printf("%d\n",len);    }    return 0;}


0 0
原创粉丝点击