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;
    }

     

     

  • 相关阅读:
    BZOJ 4033: [HAOI2015]树上染色 (树形DP)
    BZOJ 1820: [JSOI2010]Express Service 快递服务 DP
    BZOJ 4664: Count 插块DP
    BZOJ 1899: [Zjoi2004]Lunch 午餐 DP
    BZOJ 4559 [JLoi2016]成绩比较 (DP+拉格朗日插值)
    BZOJ1485 [HNOI2009] 有趣的数列 (卡特兰数)
    BZOJ 2111 / Luogu P2606 [ZJOI2010]排列计数
    20190915模拟赛
    深海机器人问题
    太空飞行计划问题
  • 原文地址:https://www.cnblogs.com/cytus/p/9458985.html
Copyright © 2011-2022 走看看