Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5243 | Accepted: 2308 |
Description
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:)
Input
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
Source
#include<stdio.h>
#include<string.h>
char s[400001];
int l;
int p[400001];
int a[400001];
void init()
{
int j;
p[1]=0;
for(int i=2;i<=l;i++)
{
j=p[i-1];
while(j>0&&s[j+1]!=s[i]) j=p[j];
if(s[j+1]==s[i]) j++;
p[i]=j;
}
}
int main()
{
while(scanf("%s",s+1)!=EOF)
{
s[0]='1';
l=strlen(s)-1;
init();
int num=0;
int j=l;
while(j>0)
{
a[num++]=j;
j=p[j];
}
for(int i=num-1;i>=0;i--)
{
if(i!=num-1) printf(" %d",a[i]);
else printf("%d",a[i]);
}
printf("/n");
}
return 0;
}