• 18.12.16 DSA Seek the Name, Seek the Fame


    描述

    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:)

    输入

    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.输出For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

    样例输入

    ababcababababcabab
    aaaaa
    

    样例输出

    2 4 9 18
    1 2 3 4 5
     1 #include <iostream>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <stack>
     5 #include <string>
     6 #include <math.h>
     7 #include <queue>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <set>
    11 #include <vector>
    12 #include <fstream>
    13 #define maxn 400005
    14 #define inf 999999
    15 #define cha 127
    16 using namespace std;
    17 
    18 int Next[maxn], m;
    19 char line[maxn];
    20 
    21 void findnext() {
    22     int i = 0, k = -1;
    23     m = strlen(line);
    24     line[m] = '-';
    25     m++;
    26     Next[0] = -1;
    27     while (i < m) {
    28         while (k >= 0 && line[i] != line[k])
    29             k = Next[k];
    30         i++, k++;
    31         if (i == m)break;
    32         Next[i] = k;
    33     }
    34 }
    35 
    36 void solve() {
    37     int k = Next[m-1];
    38     set<int>ans;
    39     while (k != 0&&k!=-1) {
    40         ans.insert(k);
    41         k = Next[k];
    42     }
    43     set<int>::iterator i1 = ans.begin(), i2 = ans.end();
    44     for (; i1 != i2; i1++)
    45         printf("%d ", *i1);
    46     printf("%d
    ", m - 1);
    47 }
    48 
    49 void init() {
    50     while (scanf("%s", line) != EOF) {
    51         memset(Next, 0, sizeof(Next));
    52         findnext();
    53         solve();
    54     }
    55 }
    56 
    57 int main()
    58 {
    59     init();
    60     return 0;
    61 }
    View Code

    KMP的运用,期中好像考了差不多的?

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    Java上传图片
    git下拉项目失败,报错 RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054
    关于Java项目启动后浏览器访问本机磁盘里的图片报错Not allowed to load local resource
    window安装Redis和springboot整合Redis进行简单使用
    Java强弱密码校验,必须包含大小写字母、数字、特殊符号
    工作记录:C# ashx生成验证码图片及校验
    springboot配置逆向工程
    mysql学习总结(二)
    ISM模型:用python实现可达矩阵求解和层级划分
    mysql学习总结(一)
  • 原文地址:https://www.cnblogs.com/yalphait/p/10127567.html
Copyright © 2020-2023  润新知