Seek the Name, Seek the Fame
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 7
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。
所以每次只需要找此段匹配的长度就好
/***************************************************************************/
代码:用了个递归,浪了一下,竟然还没PE;kmp的原理,从len开始直接相当于了位置坐标加一,慢慢体味。。。
#include<stdio.h>
#include<string.h>
const int MAXN=;
char s[MAXN];
int p[MAXN],len;
void getp(){
int i=,j=-;
p[]=-;
while(i<len){
if(j==-||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
void print(int x){
if(x<=)return;
print(p[x]);
printf("%d ",x);
}
int main(){int i,j;
while(~scanf("%s",s)){
len=strlen(s);
getp();
/*for(i=0;i<=len;i++)printf("%d ",p[i]);puts("");
j=len;
while(j>=0){
for(i=p[j];i<j;i++)printf("%c",s[i]);puts("");
j=p[j];
}*/
print(len);puts("");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=400010;
int p[MAXN]; void getp(char* s){
int len=strlen(s);
int i=0,j=-1;
p[0]=-1;
while(i<len){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
/*
void kmp(char *s,char* m,int& ans){
int len=strlen(m);
getp(s);
int len2=strlen(s);
int j=0,i=0;
while(i<len){
if(j==-1||s[j]==m[i]){
i++;j++;
if(j==len2){
ans++;
}
}
else j=p[j];
}
}
*/
int main(){
char s[MAXN];
int a[MAXN];
int tp;
while(~scanf("%s",s)){
getp(s);
tp=0;
int len=strlen(s);
while(len>0){
a[tp++]=len;
len=p[len];
}
sort(a,a+tp);
// int k=unique(a,a+tp)-a;
for(int i=0;i<tp;i++){
if(i)P_;
PI(a[i]);
}puts("");
}
return 0;
}