HDU 1358 period (KMP cycle string)

Source: Internet
Author: User
Period

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1993 accepted submission (s): 978


Problem descriptionfor each prefix of a given string s with n characters (each character has an ascii code between 97 and 126, aggressive), 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. of course, we also want to know the period K.


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


Outputfor 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

3aaa12aabaabaabaab0
 


Sample output

Test case #12 23 3Test case #22 26 29 312 4
Give you a string. If its prefix is a string with a period greater than 1, output this substring (output I + period ). solution: we need to use a conclusion. The smallest cyclic section of a string is (I-next [I]). If I % (I-next [I]) = 0, it indicates a periodic string. For more information, see the KMP least cycle section. Subject address: Period
AC code:
#include<iostream>#include<cstring>#include<string>#include<cstdio>using namespace std;char a[1000005];int next[1000005],len;void getnext(){     int i,j;     next[0]=0,next[1]=0;     for(i=1;i<len;i++)     {          j=next[i];          while(j&&a[i]!=a[j])               j=next[j];          if(a[i]==a[j])               next[i+1]=j+1;            else               next[i+1]=0;     }}int main(){     int cas=0;     while(scanf("%d",&len))     {          if(len==0) break;          scanf("%s",a);          getnext();          printf("Test case #%d\n",++cas);          for(int i=2;i<=len;i++)               if(i%(i-next[i])==0)                 if(i/(i-next[i])>1)                  printf("%d %d\n",i,i/(i-next[i]));          puts("");     }     return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.