思路:利用KMP的性质,求解:即是前缀又是后缀的字符串的长度。如果存在一个最长的前缀和后缀相等,那么次小的一定存在这个最长的里面,然后利用DFS()输出即可。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
int next[400100];
char cha[400100];
void get_next(char *tem)
{
int Len=strlen(tem);
int k=-1,j=0;
next[0]=-1;
while(j<Len)
{
if(k==-1||tem[k]==tem[j])
{
++k;
++j;
next[j]=k;
}
else
k=next[k];
}
}
void DFS(int next[],int Len)
{
if(next[Len])
DFS(next,next[Len]);
else
return ;
printf("%d ",next[Len]);
}
int main()
{
while(scanf("%s",cha)!=EOF)
{
int Len=strlen(cha);
get_next(cha);
DFS(next,Len);
printf("%d\n",Len);
}
return 0;
}