hdu 1358

Posted zquacm-875180305

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1358相关的知识,希望对你有一定的参考价值。

Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11371    Accepted Submission(s): 5313


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
 
题意:输出循环当前的位置,并且输出循环节的个数。
aabaabaabaab例子中:aa时有两个a,并且第二个a出现在第二个位置。
aabaab有两个aab,第二个aab在第六个位置结束,所以有6 2
aabaabaab有三个aab,最后一个结束在第九个位置,所以有9 3
以此类推,有12 4
 1 #include<cstdio>
 2 using namespace std;
 3 const int maxn=1000006;
 4 char str[maxn];
 5 int next[maxn];
 6 int n;
 7 
 8 void get_next()
 9 {
10     next[0]=-1;
11     int tmp=-1,i=0;
12     while(i!=n){
13         if(tmp==-1||str[i]==str[tmp])
14             next[++i]=++tmp;
15         else
16             tmp=next[tmp];
17     }
18 }
19 
20 int main()
21 {
22     int nCase=0;
23     while( ~scanf("%d",&n)&&n){
24         scanf("%s",str);
25         get_next();
26 //        for(int i=0;i<=n;i++)
27 //            printf("%d ",next[i]);
28         printf("Test case #%d
",++nCase);
29         for(int i=1;i<=n;i++){
30             int length=i-next[i];
31             if(i!=length&&i%length==0){
32                 int ans=i/(i-next[i]);
33                 printf("%d %d
",i,ans);
34             }
35         }
36         printf("
");
37     }
38     return 0;
39 }

 





以上是关于hdu 1358的主要内容,如果未能解决你的问题,请参考以下文章

E - Period HDU-1358

HDU-1358 Period 字符串问题 KMP算法 求最小循环节

HDU-1358-Period(KMP, 循环节)

hdu-1358 Period

hdu 1358

Period HDU - 1358