题目链接 http://poj.org/problem?id=2752
给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度。
#include <stdio.h>
#include <string.h>
char str[400005];
int next[400005];
int sum[400005];
int len;
int count;
void getnext( )
{
int i,j;
next[0]=-1;
i=0;j=-1;
while(i<len)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main()
{
while(scanf("%s",str)!=EOF)
{
count=0;
int i;
len=strlen(str);
getnext();
int t=next[len-1];
while(t!=-1)
{
if(str[t]==str[len-1])
sum[count++]=t+1;
t=next[t];
}
for(i=count-1;i>=0;i--)
{
printf("%d ",sum[i]);
}
printf("%d\n",len);
}
return 0;
}