zoukankan      html  css  js  c++  java
  • [POJ2752]Seek the Name, Seek the Fame

      题目链接:http://poj.org/problem?id=2752

    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
    

     

    本题求的是整个字符串中前后缀相同的子串,输出的是他们的长度。

    通过这个题又加深了对KMP的pre数组的认识:

      pre可以认为是(如pre[n] = k)字符串0-n中前k个字符和后k个字符相同。即前缀和后缀相同。

      n != 0,我们可以认为只要pre[n] != 0,那么就存在前后缀相同的子串,长度恰好是k。

      (PS:“遍历”的过程有点像并查集)

       PPS:注意输出要按照"increasing number"

    代码如下:

     

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 400010;
    16 
    17 int nb;
    18 char b[maxn];
    19 int pre[maxn];
    20 
    21 void getpre(char *b, int *pre) {
    22     int j, k;
    23     pre[0] = -1;
    24     j = 0;
    25     k = -1;
    26     while(j < nb) {
    27         if(k == -1 || b[j] == b[k]) {
    28             j++;
    29             k++;
    30             pre[j] = k;
    31         }
    32         else {
    33             k = pre[k];
    34         }
    35     }
    36 }
    37 
    38 int main() {
    39     // freopen("in", "r", stdin);
    40     while(gets(b) && strlen(b)) {
    41         int ans[maxn];
    42         int cnt = 0;
    43         nb = strlen(b);
    44         getpre(b, pre);
    45         while(pre[nb] != -1) {
    46             // printf("%d ", nb);
    47             ans[cnt++] = nb;
    48             nb = pre[nb];
    49         }
    50         for(int i = cnt-1; i >= 0; i--) {
    51             printf("%d ", ans[i]);
    52         }
    53         printf("
    ");
    54     }
    55 }
  • 相关阅读:
    jacob根据word模板和书签插入内容,在jacob1.14.3测试成功...
    在.NET上如何根据字符串动态创建控件
    rabbitvcs一款在Linux平台下类似于乌龟的SVN客户端
    平时摘录开发经验
    C#中treeview绑定xml
    ubuntu下SVN服务器安装配置
    网页中的一些常用快捷键
    python 数据类型
    python 删除空文件夹脚本
    删除指定文件的python脚本
  • 原文地址:https://www.cnblogs.com/kirai/p/4758339.html
Copyright © 2011-2022 走看看