zoukankan      html  css  js  c++  java
  • POJ-2752 再谈Seek the name,Seek the same(KMP)

    Seek the Name, Seek the Fame
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17648   Accepted: 9054

    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
    

    Source

     
     
    嗯这是我第二次写这题,对KMP也有了进一步的认识

    由于数组是从0开始存的,这有一个好处,next[ls]表示的就是后缀和前缀相同的长度,同样由于next[ls]表示的是一个长度,那么以next[ls]为下标,正好是前面的绿线的后一位,也就是说next[next[ls]]表示的是前面的绿线前缀后缀(图中用zt表示)相同的最大长度,根据next的性质前面绿线的前缀zt和后面绿线的后缀zt是一个东西,所以可以通过next[next[......next[ls]]]这样的迭代找出题目要求的所有值

     

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1000005;
     5 int ls;
     6 int next[MAX],ans[MAX];
     7 char s[MAX];
     8 void get_next(){
     9     int i,j;
    10     next[0]=-1;
    11     i=0,j=-1;
    12     while (i<ls){
    13         if (j==-1 || s[i]==s[j]){
    14             i++,j++;
    15             next[i]=j;
    16         }
    17         else j=next[j];
    18     }
    19 }
    20 int main(){
    21     freopen ("seek.in","r",stdin);
    22     freopen ("seek.out","w",stdout);
    23     int i,j;
    24     while (~scanf("%s
    ",s)){
    25         ls=strlen(s);
    26         get_next();
    27         ans[0]=0;
    28         ans[++ans[0]]=ls;
    29         i=next[ls];
    30         while (i!=-1){
    31             ans[++ans[0]]=i;
    32             i=next[i];
    33         }
    34         sort(ans+1,ans+ans[0]+1);
    35         for (i=2;i<=ans[0];i++)
    36          printf("%d ",ans[i]);
    37         printf("
    ");
    38     }
    39     return 0;
    40 }
    请理解了以后再看
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    Google笔试题
    OpenStack Ceilometer简介
    OpenStack Object Storage(Swift)架构、原理及特性
    【大话存储2读书笔记】磁盘IO的重要概念
    递归,汉诺塔游戏
    函数定义与使用
    字符串操作
    for循环:用turtle画一颗五角星
    列表、元组、字典、集合的定义、操作与综合练习
    npm 相关
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/6074026.html
Copyright © 2011-2022 走看看