POJ 2752 seek the Name, seek the Fame [KMP]

Source: Internet
Author: User
Tags integer numbers

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

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

Source

POJ Monthly--2006.01.22,zeyuan Zhu Test instructions: Given the string s, ask all the lengths of substrings that satisfy both the s prefix and the suffix of s
If the parent node of I is set to F[i], a tree will be formed.
For I's ancestor J, must satisfy S[1,j]=s[i-j+1,i]. and satisfies S[1,j]=s[i-j+1,i] J, must be the ancestor of I.
The subject is the length of all ancestors of S.
that is, the same prefix of its mismatch function must also be its same prefix (the
same prefix and suffix of the same prefix)
Note | S| must be established
.
and made an array-sized sand tea mistake.
////main.cpp//poj3461////Created by Candy on 10/19/16.//Copyright Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=4e5+5;intF[n],n;CharS[n];voidGetfail () {f[1]=0;  for(intI=2; i<=n;i++){        intj=f[i-1];  while(j&&s[i]!=s[j+1]) j=F[j]; F[i]=s[i]==s[j+1]?j+1:0; }}intans[n],m=0;voidSol () {m=0;    Getfail (); intj=F[n];  while(j) {ans[++m]=j;j=f[j];}  for(inti=m;i>=1; i--) printf ("%d", Ans[i]); printf ("%d\n", n);}intMain () {//freopen ("In.txt", "R", stdin);     while(SCANF ("%s", s+1)!=EOF) {N=strlen (s+1);    Sol (); }    return 0;}

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.