POj

来源:互联网 发布:软件机器人 编辑:程序博客网 时间:2024/06/16 21:44

传送门
//题意: 问从开头开始多长的序列前后缀是相同的.
//思路: 还是对next的理解, 理解到了这种题就是水题. 其实做多看这种题就会发现, 只要把next数组写出来, 然后对next数组进行分析和理解, 很容易出答案的.

AC Code

const int maxn = 4e5+5;int cas=1;char zc[maxn], pp[maxn];int s[maxn];int Next[maxn];void getnext(char *s, int len) {    int t1 = 0, t2;    Next[0] = t2 = -1;    while(t1 < len){        if(t2 == -1 || s[t1] == s[t2])            Next[++t1] = ++t2;        else t2 = Next[t2];    }}void solve(){    int n;    while(~scanf("%s",pp)){        getnext(pp,strlen(pp));        int top = 0;        s[top++] = strlen(pp);        int tt = strlen(pp);        while(Next[tt] != 0){            s[top++] = Next[tt];            tt = Next[tt];        }        for(int i=top-1;i>=0;i--){            printf("%d%c",s[i],i==0?'\n':' ');        }    }}