seek the Name, seek the Fame
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 16036 |
|
Accepted: 8159 |
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
This problem means that all strings in the output string have the same prefix length.
It is known that the next array contains the same prefix length in the string before the current character.
For example: next[i]=5; When starting from string to subscript i-1, with the same prefix substring of length 5
String: Ababcababababcabab
Subscript: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Character: A b A b c A b A B a B a B c a b a B
Next:-1 0 0 1 2 0 1 2 3 4 3 4 3 4 5 6 7 8 9
1, the length is 18 o'clock, the string is Ababcababababcabab, prefix ababcabab, suffix ababcabab, output 18;
2, the length is 9 o'clock, the string is Ababcabab, prefix abab, suffix abab, output 9;
3, the length is 4 o'clock, the string is abab, prefix ab, suffix AB, output 4;
4, the length is 2 o'clock, the string is AB, prefix A, suffix B, at this time the prefix is not the same, the output of the string length 2, and end;
(because the string is determined from the back-to-front one by one lookup, the output and end are found when you find the prefix that you don't want)
......
If I do not understand it, I will still use the above example to say, next[18]=9, that this string has the same prefix with a length of 9, as shown in 1 above,
Sets the string length to 9, at which point the next[9]=4 is represented by the prefix with a length of 9 and the same suffix with a length of 4.
Set the string length to 4, at this time next[4]=2; ...
Proceed in turn ....
Specific code: #include <stdio.h> #include <string.h>char s[400005];int next[400005];int ans[400005];void make_next (int len) {int I=0,j=-1;memset (next,0,sizeof (Next)), Next[0]=-1;while (I<len) {if (J==-1 | | s[i]==s[j]) {i++; j++;next[i]=j;} ELSEJ=NEXT[J];}} int main () {while (scanf ("%s", &s)!=eof) {int Len=strlen (s); Make_next (len); int I,j=0;memset (ans,0,sizeof (ans)); for (i=len;next[i]!=-1;)//until found without prefix suffix {ans[j++]=i;// Record the length of the string with the same prefix i=next[i];} for (i=j-1;i>=0;i--) printf ("%d", Ans[i]);p rintf ("\ n");} return 0;}
POJ 2752 KMP (Application of Next array)