前言
这名字好丑。。。
一波操作。。。
POJ 2752 Seek the Name,Seek the Fame
P
O
J
2752
S
e
e
k
t
h
e
N
a
m
e
,
S
e
e
k
t
h
e
F
a
m
e
链接
http://poj.org/problem?id=2752
大意
有很多组数据,每组数据给定一个字符串,从小到大输出既是前缀又是后缀的子串的长度
思路
KMP K M P 算法的 next n e x t 操作
代码
#include<cstdio>
#include<cstring>
using namespace std;
int kmp[500000],ans[500001],n,m;
char s[500000];
int main()
{
while(scanf("%s",s)!=EOF&&s[0])//输入
{
n=strlen(s);//取下长度
for(int i=1;i<n;i++) kmp[i]=0;//清0
kmp[0]=-1;//初始化
for(int i=0,j=-1;s[i];)
if(j==-1||s[i]==s[j])
{
i++;j++;
kmp[i]=j;
}
else j=kmp[j];//next
m=0;
for(int i=n;i>0;i=kmp[i])
if(kmp[i]) ans[++m]=kmp[i];//把所有答案存到ans数组里
for(int i=m;i>0;i--)
printf("%d ",ans[i]);//因为我们是倒过来存的,所以要倒过来输出
printf("%d\n",n);//自己本身也是既是前缀又是后缀的子串
}
}