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 }
  • 相关阅读:
    QT基础知识总结(一)
    二叉树知识总结(二)
    二叉树知识总结(一)
    C++ 封装,继承,多态总结
    Win API学习笔记——文件系统(二)
    Win API学习笔记——文件系统(一·)
    Go项目: package project/name is not in GOROOT
    创建`Vue-CLI`项目
    npm与yarn介绍与常用命令
    Powershell-"无法加载文件,因为在此系统上禁止运行脚本"
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4799015.html
Copyright © 2011-2022 走看看