seek the Name, seek the Fame
Time limit:2000ms Memory limit:65536k
Total submissions:13619 accepted:6755
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
Ababcababababcabab
Aaaaa
Sample Output
2 4 9 18
1 2 3) 4 5
The test instructions is a string that has the same number of prefixes and suffixes, and outputs its length from small to large respectively.
Think of the method when the problem is really bright, before learning KMP algorithm, did not feel that the next array has so much use, this back to do three KMP questions, also considered a good understanding of the next array.
A string s with a length of N. Where characters use s[0],s[1],s[2]--s[n-1].
Where Next[i] represents the meaning of the string s before position I, how many and the string from the beginning of the number of equal numbers, specifically what meaning, that is, say next[i]=k, description s[0]s[1]s[2]–s[k-1] = s[i-k]–s[ I-3]S[I-2]S[I-1]. This is also the method of seeking next array, judging s[i]==s[j], then i++,j++. The last assignment is next[i]=j. So Next[i] actually represents the case before the character I.
So I next[n]=k the words (n is the length of the entire string), which means that the maximum number of K characters in the string satisfies s[0]s[1]s[2]–s[k-1] = s[n-k]–s[n-3]s[n-2]s[n-1], And isn't that just one of the things that you're satisfied with?
After asking for this next[n]=k, the next requirement is next[k], because you are satisfied s[0]s[1]s[2]–s[k-3]s[k-2]s[k-1] = s[n-k]--s[n-3]s[n-2]s[n-1 ] This condition, that is, the suffix string is the same, I only need to narrow the range, narrow to s[0]–s[k-1], next[k] can be. After that, and so on, constantly query, know next[]=0. Because the topic requires from large to small output, so each record down, and then re-output can.
Code:
#include <iostream>#include <vector>#include <string>#include <cstring>#include <algorithm>#pragma warning (disable:4996)using namespace STD;Chara[400005];intnext1[400005];intLen vector<int>ResultvoidCal () {inti,j=-1; next1[0]=-1; for(i=0; i<len+1;) {if(j==-1|| A[i]==a[j]) {i++; j + +; Next1[i]=j; }Else{J=NEXT1[J]; } }}intMain () { while(Cin>>a) {len =strlen(a); Cal ();inttemp = Len; while(Next1[temp]) {Result.push_back (next1[temp]); TEMP=NEXT1[TEMP]; } sort (Result.begin (), Result.end ());intI for(i=0; I<result.size (); i++) {cout<<result[i]<<" "; }cout<<len<<endl; Result.clear (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2752:seek The Name, Seek the Fame