zoukankan      html  css  js  c++  java
  • poj 2752

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

    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

     
    (一道好题,让我更加理解了next数组。
    题意:给一个字符串,找所有子串满足既是母串的前缀又是母串的后缀。
    解题思路:因为若满足是母串的后缀,必定最后一个字母是a[len-1];又因为nex[len-1]的前缀一定与len-1的前面一部分相同(这两个因为都是nex数组的性质。所以只需要对nex[len-1]递归下就好了。
    如果还是不理解可以对着nex数组推一下。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cstdio>
     5 #include <cstdlib>
     6 #include <cmath>
     7 using namespace std;
     8 typedef long long ll;
     9 const int maxn = 400000+5;
    10 char a[maxn];
    11 int nex[maxn];
    12 void getn(int len)
    13 {
    14     int j=-1;
    15     nex[0]=-1;
    16     for(int i=1;i<len;++i)
    17     {
    18         while(j>-1 && a[j+1]!=a[i]) j=nex[j];
    19         if(a[j+1]==a[i]) j++;
    20         nex[i]=j;
    21     }
    22 }
    23 void print(int n)
    24 {
    25     if(n==-1) return ;
    26     print(nex[n]);
    27     printf("%d ",n+1);
    28     
    29 }
    30 int main()
    31 {
    32     while(~scanf("%s",a))
    33     {
    34         int len=strlen(a);
    35         getn(len);
    36         print(len-1);
    37         printf("
    ");
    38     }
    39     return 0;
    40 }
    View Code
  • 相关阅读:
    ElasticSearch(ES)学习笔记
    Lucene學習日志
    velocity代码生成器的使用
    springboot学习笔记
    springmvc json 类型转换错误
    在做del业务时,传递参数,和接口中入参注释
    做add添加业务时,字符集乱码,form标签库,button的href 问题,添加后页面跳转,forward,redirect 。定制错误输出
    mybatis中联合查询的返回结果集
    mybatis分页,绝对路径的2种写法
    maven导入项目时报错,配置应用程序监听器[org.springframework.web.context.ContextLoaderListener]错误
  • 原文地址:https://www.cnblogs.com/zmin/p/7944451.html
Copyright © 2011-2022 走看看