Seek the Name, seek the Fame
Time Limit: 2000MS |
Memory Limit: 65536K |
Total Submissions: 16388 |
Accepted: 8330 |
Description
The little cat is so famous, which many couples tramp over Hill and Dale to Byteland, and asked the little cat to give name s 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 a 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 isn't 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 is {' A ', ' ala ', ' Alala '}. Given the string S, could the little cat to write a program to calculate the length of possible prefix-suffix str Ings 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, contains the string S described above.
Restrictions:only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with an integer numbers in increasing order, denoting the possible length of the new Baby ' s name.
Sample Input
Ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5
Source
POJ Monthly--2006.01.22,zeyuan Zhu
Test instructions: To find the same prefix and suffix, from small to large output their length. Procedure: KMP Next (fail) array of simple application, from Next[len] has been recursive to the end, the output length can be. The next array recursive process must be strictly decremented, so we can not order, direct reverse output can be. The code is as follows.
/*Source Code problem:2752 user:acloliconmemory:3188k time:110mslanguage:g++ result:acceptedsour Ce Code*/#include<cstdio>#include<cstring>#include<iostream>#defineMAXL 400050using namespacestd;intFail[maxl], Len, ans, tot;intA[MAXL];CharC[MAXL];voidGetfail () {inti =0, j =-1; fail[0] = -1; while(I! =Len) { if(j = =-1|| C[i] = = C[j]) Fail[++i] = + +J; Elsej =Fail[j]; }} intMain () { while(~SCANF ("%s", C)) {ans=0, tot =0; Len=strlen (c); Getfail (); Ans=Fail[len]; A[tot+ +] = len;//the whole string is definitely while(Ans >0) {A[tot++] =ans; Ans=Fail[ans]; } for(inti = tot-1; I >=0; i--) cout << a[i] << (i = =0?'\ n':' '); }}
Simple application of POJ2752:KMP