zoukankan      html  css  js  c++  java
  • poj 2752 Seek the Name, Seek the Fame (KMP)

    Description

    The little cat is so famous, that 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 at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an 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 not 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 strings 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 single line 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 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
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 char s[1000000];
     5 int a[1000000];
     6 int next[1000000];
     7 void getnext()
     8 {
     9     int i=0,j=-1;
    10     next[0]=-1;
    11     while (s[i])
    12     {
    13         if (j==-1||s[i]==s[j])
    14         {
    15             i++;
    16             j++;
    17             next[i]=j;
    18         }
    19         else j=next[j];
    20     }
    21 }
    22 int main()
    23 {
    24     int i,j;
    25     while (~scanf("%s",&s))
    26     {
    27         getnext();
    28         j=0;
    29         for (i=strlen(s);i!=0;)
    30         {
    31             a[j++]=next[i];
    32             i=next[i];
    33         }
    34         for (i=j-2;i>=0;i--)
    35          printf("%d ",a[i]);
    36          printf("%d
    ",strlen(s));
    37     }
    38 }
  • 相关阅读:
    【DOS命令】type
    【shell】$符号操作
    【C++百科】STL(Standard Template Library) 标准模板库简介
    【配置与安装】SSH 密钥类型的的选择(RSA, DSA or Other)
    【百科通识】CPU x86/x86-64/x64/i386
    【配置与安装】SSH远程开发
    【百科通识】.bat文件
    【vi】模式切换
    【vim】撤销与回退
    【C++】智能指针
  • 原文地址:https://www.cnblogs.com/pblr/p/4754286.html
Copyright © 2011-2022 走看看