PKU 2752 seek the Name and seek the Fame (KMP)

Source: Internet
Author: User
Tags integer numbers
Seek the Name and seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total submissions: 9783 accepted: 4693

Description the little cat is so famous, which many couples tramp over Hill and Dale to Byteland, and asked the little Cat to give names to their newly-born babies. They seek the name, and in 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: 
br> 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 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 are {' A ', ' ala ', ' Alala '}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix str Ings of S? (He might thank your by giving your baby a name:)  

Input the input contains a number of test cases. Each test case is occupies a single and that 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 1 2 3 4 5

Source POJ Monthly--2006.01.22,zeyuan Zhu
To find the length of the substring that is both the prefix of the string and the suffix of the string: Use the KMP value flexibly and save the data with the stack. The value of next is known to represent the next "I"-1 characters and the prefix of the next "I"-1 characters in front of the position I, and the equal pass of the iteration proves that each I=next "i+1"-1 is a substring that is both a prefix and a suffix

#include <stdio.h>
#include <string.h>
char s[400005];
int next[400005],sta[400005],all;
void Get_next ()
{
    int len=strlen (s+1), i=1,j;

    next[1]=j=0;
    while (I<=len)
    {
        if (j==0| | S[I]==S[J]) next[++i]=++j;
        else j=next[j];
    }
void Solve ()
{
    int len=strlen (s+1), I;

    All=0,sta[all++]=len;
    for (i=len;i>1;)
    {
        if (next[i+1]>1) sta[all++]=next[i+1]-1;
        I=next[i+1]-1
    }
}
void print ()
{
    int i;
    for (i=all-1;i>0;i--) printf ("%d", Sta[i]);
    printf ("%d\n", Sta[i]);
int main ()
{
    while (scanf ("%s", &s[1]) >0)
    {
        get_next ();
        Solve ();
        Print ();
    }
    return 0;
}


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.