Title Link: http://poj.org/problem?id=2752
Seek the Name, seek the Fame
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 17393 |
|
Accepted: 8907 |
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
The length of a substring given a string output that is both a prefix and a suffix
The value of Next[i] indicates the same prefix and suffix as the substring length before I in the string is Next[i]
Subscript |
1 |
2 |
3 |
4 |
5 |
|
7 |
8 |
9 |
Ten |
One |
" |
|
|
- |
|
|
18 |
|
|
A |
B |
A |
B |
C |
A |
B |
A |
B |
A |
a |
B |
C |
A |
B |
A |
B |
|
|
0 |
0 |
" |
2 |
0 |
|
2 |
3 |
4 |
3 |
|
4 |
5 |
6 |
|
|
9 |
|
AC Code:
1#include <stdio.h>2#include <string.h>3 intnext[400010];4 Charstr[400010];5 voidGetNext ()//first find the next array6 {7 inti =0, j =-1;8next[0] = -1;9 intLen =strlen (str);Ten while(I <len) One { A if(j = =-1|| str[i]==Str[j]) - { -++i; the++J; -Next[i] =J; - } - Else +j =Next[j]; - } + } A intMain () at { - intnum[400010],i,k; - while(SCANF ("%s", str)! =EOF) - { - intLen =strlen (str); - GetNext (); innum[0] =Len; -K =1; to while(next[num[k-1]])//with the next array will be both the length of the substring prefixed with the suffix + { -Num[k] = next[num[k-1]]; theK + +; * } $ for(i = k-1; i >0; I--)Panax Notoginsengprintf"%d", Num[i]); -printf"%d\n", num[0]); the } + return 0; A}
View Code
POJ 2752 seek the Name, seek the Fame