• 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.

  • 相关阅读:
    codeforces cf round#505(based on vk cup 2018 final) C. Plasticine zebra
    cf round 505(div1+div2)based on VK cup 2018 final B. Weakened Common Divisor
    codeforces AIM Tec round 5(div1+div2) C. Rectangles
    codeforces cf AIM tech round5(rated for div1+div2) B. Unnatural Conditions
    C. Maximal Intersection codeforces round#506(div3)
    uva 725 Division(暴力枚举) 解题心得
    POJ 2386 Lake Counting_steven 解题心得
    UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS) 解题心得
    HDU1372:Knight Moves(BFS) 解题心得
    POJ 2255 Tree Recovery 解题心得
  • 原文地址:https://www.cnblogs.com/muzu/p/7141931.html
Copyright © 2020-2023  润新知