http://poj.org/problem?id=2752
Seek the Name, seek the Fame
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 14611 |
|
Accepted: 7320 |
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
#include <stdio.h>#include<string.h>#include<iostream>#include<stack>using namespacestd;#defineN 1000007#defineMax (A, B) (A>B?A:B)intNext[n];CharS[n];voidFindNext (intSlen) { intI=0, j=-1; next[0] = -1; while(i<Slen) { if(j==-1|| s[i]==S[j]) next[++i] = + +J; ElseJ=Next[j]; }}intMain () { while(SCANF ("%s", S)! =EOF) { intSlen, N; Slen=strlen (S); N=Slen; FindNext (Slen); Stack<int>p; while(n) {Q.push (next[n]); N=Next[n]; } q.pop (); while(Q.size ()) {printf ("%d", Q.top ()); Q.pop (); } printf ("%d\n", Slen); } return 0;}
View Code
(KMP) Seek the Name, seek the Fame--POJ--2752