• poj 2752Seek the Name, Seek the Fame


    Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

     Status

    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

    ______________________________________________________________________________________________________________________________________________________

    题目大意:给定字符串S,求出S的所有公共前后缀的长度(包含自身)

    KMP求出next[],S的公共前后缀中前缀的公共前后缀也是S的公共前后缀。用此循环即可。
    _______________________________________________________________________________________________________________________________________________________

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 const int maxl=400010;
     7 char s[maxl];
     8 int next[maxl],l;
     9 int ans[maxl],js=0,ll;
    10 void getnext()
    11 {
    12     l=strlen(s);
    13     next[0]=-1;
    14     for(int j,i=1;i<l;i++)
    15     {
    16         j=next[i-1];
    17         while(s[i]!=s[j+1] && j>=0)j=next[j];
    18         next[i]=s[i]==s[j+1]?j+1:-1;
    19     }
    20 }
    21 int main()
    22 {
    23     while(~scanf("%s",s))
    24     {
    25         getnext();
    26         js=0;ll=l-1;
    27         while(next[ll]!=-1)
    28         {
    29             ans[js++]=next[ll];
    30             ll=next[ll];
    31         }
    32         while(js>0)printf("%d ",ans[--js]+1);
    33         printf("%d
    ",l);
    34     }
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    sklearn 的 metrics
    转载:spring boot 中使用 jpa以及jpa介绍
    springboot 事件监听(@EventListener实现)
    多线程:创建线程和线程的常用方法
    缓存穿透、缓存击穿、缓存雪崩区别和解决方案
    不定义新变量,交换两个变量的值
    理解WebSocket心跳及重连机制
    SpringBoot实现WebSocket
    批量打包成ZIP压缩文件
    RocketMQ入门教程
  • 原文地址:https://www.cnblogs.com/gryzy/p/6527898.html
Copyright © 2020-2023  润新知