zoukankan      html  css  js  c++  java
  • 【HDU 1358 Period】

                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                             Total Submission(s): 9449    Accepted Submission(s): 4530
    Problem Description

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

    Input

    The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.

    Output

    For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

    Sample Input

    3 aaa 12 aabaabaabaab 0

    Sample Output

    Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4

    Recommend

    JGShining

     

    题解:

    ①题目要求求出每一个前缀的循环节(只循环一次不算),可以使用最小循环节长度=n-next[n]这一性质

    ②利用上述质,只需要判断循环节长度是否能够整除当前前缀长度并至少不含2个循环节即可输出结果

    #include<stdio.h>
    #define go(i,a,b) for(int i=a;i<=b;i++)
    const int N=1000003;int m,f[N],j,n,T;char P[N];
    int main()
    {
        while(scanf("%d",&m),m)
        {
    	   scanf("%s",P);printf("Test case #%d
    ",++T);f[0]=f[1]=0;
    
    	   go(i,1,m-1){j=f[i];while(j&&P[j]!=P[i])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
    	   go(i,1,m-1){n=i+1-f[i+1];if((i+1)%n==0&&(i+1)/n>1)printf("%d %d
    ",i+1,(i+1)/n);}
    	   puts("");
        }
        return 0;
    }//Paul_Guderian
    
    

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    我曾随迷失的航船沉没,陷入璀璨虚空的碎梦。

    沉入乱欲冰封的深谷,随烂漫的星群沉没。————汪峰《再见青春》

  • 相关阅读:
    面向对象进阶
    20191011作业
    2019.10.10作业
    类的继承
    面向对象
    2019.10.09作业
    pandas模块
    [BZOJ 2190][SDOI2008]仪仗队(欧拉函数)
    [BZOJ 2729][HNOI2012]排队(组合数学+高精)
    [BZOJ 1491][NOI2007]社交网络(Floyd)
  • 原文地址:https://www.cnblogs.com/Damitu/p/7651796.html
Copyright © 2011-2022 走看看