题目大意:
给你一个字符串,要求你求出所有满足既是前缀又是后缀的子串的长度并升序输出
#include<stdio.h>
#include<string.h>
#define N 400009
int next[N];
int ans[N/2];
void getNext(char *p,int n)
{
int j = -1, i = 0;
next[0] = -1;
while(i < n)
{
if(j == -1 || p[i] == p[j])
{
j++; i++;
next[i]=j;
}
else
{
j = next[j];
}
}
}
int main()
{
char buff[N];
int len,i,j,k;
while(gets(buff)){
int len=strlen(buff);
getNext(buff,len);
ans[0]=len;
i=next[len];
j=0;
while(i){
ans[++j]=i;
i=next[i];
}
while(j>=0){
if(j!=0)
printf("%d ",ans[j--]);
else
printf("%d\n",ans[j--]);
}
}
}