zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    Seek the Name, Seek the Fame

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 23798   Accepted: 12431

    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

     


      分析:

      又是一道神奇的$KMP$题,又是需要掌握$next$数组的正确姿势。(咦,我为什么要说又?)

      首先一个字符串它自己肯定算一个答案。求出$next$数组然后从$next[n]$开始计数,只要当前$next[]$不为$0$,那么就记录新答案为$next[]$值,然后继续。至于正确性可以自己证明,只要深入理解了$next$数组,就不难理解了。

      (当然,这题可以用$hash$做,且复杂度也差不多是$O(n)$的)

      Code:

     

    //It is made by HolseLee on 11th Aug 2018
    //POJ2752
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<iomanip>
    #include<algorithm>
    using namespace std;
    
    const int N=2e6+7;
    int n,nxt[N],k,ans[N],cnt;
    char s[N];
    
    int main()
    {
        while(scanf("%s",s)!=EOF){
            n=strlen(s);
            nxt[0]=nxt[1]=0;k=0;
            for(int i=1;i<n;++i){
                while(k&&s[i]!=s[k])
                k=nxt[k];
                nxt[i+1]=(s[i]==s[k]?++k:0);
            }
            k=nxt[n];cnt=1;ans[1]=n;
            while(k){
                ans[++cnt]=k;
                k=nxt[k];
            }
            for(int i=cnt;i>=1;--i)
            printf("%d ",ans[i]);
            printf("
    ");
        }
        return 0;
    }

     

     

  • 相关阅读:
    H5定位终极解决方案
    软帝学院教你使用cookie法,查看最近看过的书
    你真的会用Gson吗?Gson使用指南(一)
    Java程序员应当知道的10个面向对象设计原则
    java获取当前月第一天和最后一天,上个月第一天和最后一天
    正则基础教程一些冷门的知识
    爆笑的程序员梗,笑死人不偿命!
    java字符串操作扩充:灵活截取字符串
    如何分析及处理 Flink 反压?
    与君初相识,犹如故人归
  • 原文地址:https://www.cnblogs.com/cytus/p/9458985.html
Copyright © 2011-2022 走看看