Seek the Name, Seek the Fame
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12040 | Accepted: 5924 |
Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
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:)
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
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
题意:找出前缀等于后缀的子串所有长度 利用EKMP求出原串的最长公共前缀长度,如果下标加最长公共前缀长度等于原串长度 最长公共前缀长度就是所找的长度 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; char s[400010],str[400010]; int num[400010],next[400010],extend[400010],p; void EKMP() { int len1=strlen(str); int k=1,j=0,l,len; next[0]=0; while(j<len1&&str[j]==str[j+1])//求next数组 j++; next[1]=j; for(int i=2;i<len1;i++) { len=next[k]+k-1//扫过的最远的地方 l=next[i-k];//重复的长度 if(l+i-1<len) next[i]=l;//如果重复的长度加上下标小于最远的地方,next[i]就为重负的长度 else { j=max(0,len-i+1);//j为第一个未知字符的下标 while(j<len1&&str[j+i]==str[j]) j++; next[i]=j; k=i;//更新最远的下标 } } k=0,j=0; while(j<len1&&str[j]==s[j]) j++; extend[0]=j; if(j==len1) num[p++]=j; for(int i=1;i<len1;i++)//求最长公共前缀 { len=extend[k]+k-1; l=next[i-k]; if(l+i-1<len) { extend[i]=l; } else { j=max(0,len-i+1); while(i+j<len1&&s[j+i]==str[j]) j++; if(i+j==len1) num[p++]=j; extend[i]=j; k=i; } } } int main() { while(scanf("%s",s)!=EOF) { int len=strlen(s); for(int i=0;i<=len;i++) str[i]=s[i]; if(len<2)//特判 cout<<"1"; else { p=0; EKMP(); printf("%d",num[p-1]);//倒序输出 for(int i=p-2;i>=0;i--) printf(" %d",num[i]); } cout<<endl; } return 0; }