zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    经验光照模型整理
    桥接模式下主机ping不通虚拟机,虚拟机能ping主机?kdump failed
    Vue项目目录结构分析
    interface{} 指针 bool 取值
    mac read-only file
    01Prism WPF 入门实战
    C#实现约瑟夫环数学问题
    SqlServer根据某列来分组后,再依据另一列来排序,选取第一行,后再依据另一列进行筛选,之后再统计数量
    2.算法分析-时间复杂度和空间复杂度
    1.数据结构和算法概述
  • 原文地址:https://www.cnblogs.com/muzu/p/7141931.html
Copyright © 2011-2022 走看看