• Period


     
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 18043   Accepted: 8742

    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 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
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char s[1000001];
    int ne[1000001];
    int main(){int i,j,n,t=0,move;
        while(scanf("%d",&n)&&n){memset(ne,0,sizeof(ne));
            scanf("%s",s+1);printf("Test case #%d
    ",++t);j=0;
                printf("ne:0,i:1
    ");
              for(int i=2,j=0;i<=n;i++){
                 while(s[i]!=s[j+1]&&j>0)j=ne[j];
                 if(s[i]==s[j+1])ne[i]=++j;
                 //printf("ne:%d,i:%d
    ",ne[i],i);----------------if you really belive the sience
                 //you can have a try last_line_test
                 //-------------above want ne_map
                 //The meaning of ne_map
                 //ne[i] mark from 1 to i's word,prefix and suffix has the longest_same_length
                 //for example
                 //aba ,prefix and suffix both have a,the longest_same_length is 1
                 //abab,prefix and suffix both have ab,the longest_same_length is 2
                 //maybe you want to ask :what is prefix,what is suffix
                 //prefix is it has group_case except last letter
                 //suffix is it has group_case except first letter
                 //for example ,a word abcba
                 //it's prefix  is a,ab,abc,abcb
                 //it's suffix is a,ba,cba,bcba
                 //and they have one case in same(a) ,so their ne_num is 1
                 move=i-ne[i];
                 //-------------------------------------------------------
                 // KMP algorithm use move to move one string,and check this string with another string
                 //it's easier and quicker to find out coincidence(just move more quickly)
                 //if you have not learnt KMP,I think you had better learn it
                 //it's really a fast and convinient algorithm
                 //-------------------------------------------------------
                 if(ne[i]&&(i%move==0)) printf("%d %d
    ",i,i/move);
                 //if it has same_length|and|have repetend,
                 //just printf the ask i and the repetend_length
                 //you may wonder why if i%move==0 this part have repetend
                 //let me tell you
                 //as we know the repetend_part are always even number
                  //for example:aabaab
                  //the  ne_num:010123
                 //the move_num:113333
                 //think it over on the if_step then run the program with the data_given
                 //then you will find that is true
                 //certify:
                 //to prove this is true ,you should start with the word
                 //for example:
                 //aa
                 //it's ne_num is 1(a)
                 //cut it
                 //then the word become a
                 //abab
                 //it's ne_num is 2(ab)
                 //cut it
                 //then the word become ab
                 //ababab
                 //it's ne_num is 4(abab)
                 //cut it
                 //then the word become ab
                 //abcabc
                 //it's ne_num is 3(abc)
                 //cut it
                 //then the word become abc
                 //abba
                 //it's ne_num is 1(a)
                 //cut it
                 //then the word become abb
                 //as we see,a repetend_word has a repetend_word_part
                 //after cut,the repetend_word always can % cut_part==0
                 //because except the repetend_word ,it has no other part
                 //certify over!!!!!
            }printf("
    ");
        }return 0;
    }

    program is just above.

  • 相关阅读:
    解析python数据后用html输出
    python 自动化测试HTTP接口
    python 自动化对比返回结果
    Java 基础知识 练习
    Java 菜鸟学习之 script脚本语句
    java语言的认识
    Script 语言的简单练习题 乘法口诀
    Script 简单语句的练习题
    Java菜鸟培训第二天
    36个号码随机摇奖编码
  • 原文地址:https://www.cnblogs.com/muzu/p/7141931.html
Copyright © 2020-2023  润新知