zoukankan      html  css  js  c++  java
  • Seek the Name, Seek the Fame (poj2752

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

    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

    题意:给一字符串让你找出其前缀和后缀相等的可能长度都是多少,升序输出。
    简单模板一遍过
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 #define N 400008
     9 
    10 char w[N];
    11 int next[N];
    12 int sum[N];
    13 
    14 void getNext()
    15 {
    16     int j, k;
    17     int i = strlen(w);
    18     j = 0;
    19     k = -1;
    20     next[0] = -1;
    21 
    22     while(j < i)
    23     {
    24         if(k == -1 || w[j] == w[k])
    25         {
    26             j++;
    27             k++;
    28             next[j] = k;
    29         }
    30         else
    31             k = next[k];
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     while(~scanf("%s", w))
    38     {
    39         memset(next, 0, sizeof(next));
    40         memset(sum, 0, sizeof(sum));
    41 
    42         getNext();
    43         int l1, k;
    44         l1 = strlen(w);
    45 
    46         k = 0;
    47         sum[k++] = l1;
    48 
    49         while(l1 > 1 && next[l1])
    50         {
    51             sum[k] = next[l1];
    52             l1 = sum[k];
    53             k++;
    54         }
    55 
    56         sort(sum, sum+k);
    57         printf("%d", sum[0]);
    58 
    59         for(int i = 1; i < k; i++)
    60         {
    61             printf(" %d", sum[i]);
    62         }
    63 
    64         puts("");
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    《大道至简》读后感
    第一周学习总结-Java
    c++与java的几个不同点
    单调队列 滑动窗口模型
    计算空间
    关于dp初始化问题
    康托展开小结-
    Vm-Ubuntu下配置Qt开发环境
    C++学习013多态
    C++学习012友元
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4799015.html
Copyright © 2011-2022 走看看