POJ 2752 seek the Name, seek the Fame (KMP's next function applies)

Source: Internet
Author: User
Tags integer numbers

Seek the Name, seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14188 Accepted: 7068

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

Test instructions: Give a string s, judging at what subscript, prefix and suffix equal, output prefix and suffix equal points.

Analysis: A clever use of the next array

The meaning of the next array is that the current subscript precedes the K character and the beginning of the first K-character equal

So there will be xy=ab (with XY for this section of X~y), then Next[b]=y, then the next time from the Y position to start matching

If Xk=wy, because the Xy=ab, therefore wy=lb, therefore xk=lb, has obtained the prefix and the suffix is equal.

So match until Next[i]=-1, period record subscript

#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<string>#include<iostream>#include<cstring>#include<cmath>#include<stack>#include<queue>#include<vector>#include<map>#include<stdlib.h>#include<algorithm>#defineLL __int64using namespacestd;Const intmaxn=100000+5;CharSTR[MAXN];intNEX[MAXN];intANS[MAXN];intN;voidGetNext () {intj=0, k=-1; nex[0]=-1;  while(j<N) {if(k==-1|| str[j]==str[k]) nex[++j]=++K; Elsek=Nex[k]; }}intMain () {//freopen ("In.txt", "R", stdin);     while(SCANF ("%s", str)! =EOF) {N=strlen (str);        GetNext (); inti=n,tot=0;  while(nex[i]!=-1) {Ans[tot++]=i; I=Nex[i]; }         for(inti=tot-1; i>=0; i--)        {            if(i==tot-1) printf ("%d", Ans[i]); Elseprintf"%d", Ans[i]); } printf ("\ n"); }    return 0;}
View Code

POJ 2752 seek the Name, seek the Fame (KMP's next function applies)

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.