poj 2752Seek the Name, Seek the Fame
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
______________________________________________________________________________________________________________________________________________________
题目大意:给定字符串S,求出S的所有公共前后缀的长度(包含自身)
KMP求出next[],S的公共前后缀中前缀的公共前后缀也是S的公共前后缀。用此循环即可。
_______________________________________________________________________________________________________________________________________________________
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std; 6 const int maxl=400010; 7 char s[maxl]; 8 int next[maxl],l; 9 int ans[maxl],js=0,ll; 10 void getnext() 11 { 12 l=strlen(s); 13 next[0]=-1; 14 for(int j,i=1;i<l;i++) 15 { 16 j=next[i-1]; 17 while(s[i]!=s[j+1] && j>=0)j=next[j]; 18 next[i]=s[i]==s[j+1]?j+1:-1; 19 } 20 } 21 int main() 22 { 23 while(~scanf("%s",s)) 24 { 25 getnext(); 26 js=0;ll=l-1; 27 while(next[ll]!=-1) 28 { 29 ans[js++]=next[ll]; 30 ll=next[ll]; 31 } 32 while(js>0)printf("%d ",ans[--js]+1); 33 printf("%d\n",l); 34 } 35 return 0; 36 }