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
  • 相关阅读:
    莎士比亚名言
    庄子名言
    Javascript: 通过图片url获取图片blob对象
    toast提示 封装
    maya模型导入UE4,如何做到等比导入
    GPU渲染和CPU渲染农场成本对比
    C4D渲线框
    C4D教程笔记——高科技线框3D模型全息投影 X-Wing Hologram Animation
    arnold和redshift渲染器
    使用Redshift渲染器,怎么选电脑配置!
  • 原文地址:https://www.cnblogs.com/zmin/p/7944451.html
Copyright © 2011-2022 走看看