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

     

     

  • 相关阅读:
    PowerDesigner使用教程 —— 概念数据模型 (转)
    面板Panel:1.认识Ext.Panel(转)
    批量替换一个数据库中所有表中所有记录
    Ext.data库中几个常用类的原理及其使用 (转)
    也谈C#.NET防止SQL注入式攻击
    jQuery1.3新特性深度分析(Event)
    Sql实现Split
    SQL Server 清除数据库日志脚本(转)
    ExtJS之面向对象编程基本知识
    实战 SQL Server 2005 镜像配置
  • 原文地址:https://www.cnblogs.com/cytus/p/9458985.html
Copyright © 2011-2022 走看看