Click to open the topic
Seek the Name, seek the Fame
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 12651 |
|
Accepted: 6214 |
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
Approximate test instructions:
Give a string str, find out how many substrings exist in Str, so that these substrings are both str prefix and str suffix. Output the length of these substrings in turn from small to large
The idea of solving problems: the meaning of next array in understanding KMP algorithm, recursive solution
#include <cstdio> #define MAXN 400002char str[maxn];int next[maxn],len,s;void GetNext () { int i=0,j=-1; Next[0]=-1; while (Str[i]) { if (j==-1| | Str[i]==str[j]) { ++i;++j;next[i]=j; } else j=next[j]; } Len=i;} void Getval (int n) { if (next[n]==0) return; Getval (Next[n]); printf ("%d", Next[n]);} int main () { while (scanf ("%s", str) ==1) { GetNext (); Getval (len); printf ("%d\n", Len); } return 0;}
POJ 2752 seek the Name, seek the Fame "KMP"