POJ 2752 seek the Name, seek the Fame KMP

Source: Internet
Author: User
Tags integer numbers

Seek the Name, seek the Fame

Time limit:20 Sec Memory limit:256 MB

Topic Connection http://poj.org/problem?id=2752
DescriptionThe Little cat is so famous, which many couples tramp over Hill and Dale to Byteland, and asked the little cat T o give names 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:) inputthe 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 Output2 4 9 18
1 2 3 4 5HINT

Test Instructions

Give you a string, if the string exists a prefix string of length n, and a suffix string of length n, and the two strings are equal, then output their length n. Find out all the length n.

Exercises

Get_next () in the KMP. For the prefix function next[] and further understanding, Str[1]~~str[next[len]] The content must be able to match Str[1+len-next[len]]~~str[len] (Figure 1). And then we recycle next, because next, in Figure 2, if the left red string matches the left green string, then the left red string ratio matches the right green string, because the left red string in Figure 1 is exactly equal to the right red string. It is guaranteed that each string can be matched to the last letter, that is, to get a prefix equal to the suffix. But the length of the string is constantly decreasing.

(It's a turning point, it's very clear)

Code:

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 500001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10;    while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;}  while(x>0) ch[++num]=x%Ten, x/=Ten;  while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************strings;intP[MAXN];intANS[MAXN];intMain () { while(cin>>s) {memset (ans,0,sizeof(ans)); Memset (P,0,sizeof(p)); intlen=s.size (); p[0]=-1; intj=-1, i=0;  while(i<Len) {            if(j==-1|| s[i]==S[j]) {i++,j++; P[i]=K; }            Elsej=P[j]; }        intCnt=0; intt=p[len-1];  while(t!=-1) {cout<<t<<Endl; if(s[t]==s[len-1]) ans[cnt++]=t+1; T=P[t]; }         for(inti=cnt-1; i>=0; i--) cout<<ans[i]<<" "; cout<<len<<Endl; }}

POJ 2752 seek the Name, seek the Fame KMP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.