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 }
  • 相关阅读:
    MFC加载图片
    动态数组类
    MFC程序打包方法
    如何在C++中使用动态三维数组
    Ansys热应力计算
    像使用数据库一样使用xml
    过年回家的一点感想
    前后端框架和设计模式
    国外支付PayPal
    可重用的管理后台代码
  • 原文地址:https://www.cnblogs.com/pblr/p/4754286.html
Copyright © 2011-2022 走看看