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 }
    请理解了以后再看
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    答《同样 25 岁,为什么有的人事业小成、家庭幸福,有的人却还在一无所有的起点上?》
    [面试记录-附部分面试题]2014第一波的找工作的记录
    项目总结(二)->一些常用的工具浅谈
    项目总结(一)->项目的七宗罪
    Android学习笔记(三)Application类简介
    Android学习笔记(二)Manifest文件节点详解
    Android学习笔记(一)Android应用程序的组成部分
    Mac下搭建Eclipse Android开发环境
    Android开发必知--自定义Toast提示
    正则表达式(一)
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/6074026.html
Copyright © 2011-2022 走看看