【POJ2752】seek the name,seek the fame

来源:互联网 发布:小米电视软件推荐 编辑:程序博客网 时间:2024/06/04 20:01

题目链接:http://poj.org/problem?id=2752
题解:
一直跳next就好

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[400010]; int nxt[400010],stack[400010];void Get_nxt(char *s,int len,int nxt[]){    int j=0;    nxt[1]=0;    for(int i=2;i<=len;i++)    {        while(j&&s[i]!=s[j+1]) j=nxt[j];        j+=(s[i]==s[j+1]);        nxt[i]=j;    }}int main(){    while(~scanf("%s",s+1))    {        int len=strlen(s+1);        Get_nxt(s,len,nxt);        int p=nxt[len],top=0;        while(p)        {            stack[++top]=p;            p=nxt[p];        }        while(top)            printf("%d ",stack[top--]);        printf("%d\n",len);     }    return 0;}
原创粉丝点击