题目链接:http://poj.org/problem?id=2752
#include <stdio.h>
#include <string.h>
char s[400005];
int next[400005];
int sum[40005];
int main()
{
int i,j,len;
while(scanf("%s",s) != EOF)
{
i = 0;
j = -1;
next[0] = -1;
len = (int)strlen(s);
while(i < len)
{
if(j == -1 || s[i] == s[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
j = 0;
for(i = len; next[i] != -1; i = next[i])
sum[j++] = i;
printf("%d",sum[j-1]);
for(i = j-2; i >= 0; --i)
printf(" %d",sum[i]);
printf("\n");
}
return 0;
}