1. 程式人生 > >poj 1961 Period

poj 1961 Period

ins size col class queue bsp pst tdi written

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
題意:(註意輸出)
給定字符串S,求1-i的字符串的循環節(循環節長度要大於1)
解:
假設要求整個
字符串的循環節 poj 2406
len為整個字符串的長度
想一想next[i]的含義 每次匹配失敗後,都要回到next[i]開始重新匹配 也就是說 next[i]+1 -> len 這段字符 (令k為這段字符的長度)和 1 -> 1+k 這段字符 是相同的 OK,如果(len%(len-next[len])==0) ==> 存在 len/(len-next[len]) 段 解 否則只有1段
那麽求1-i的字符串的循環節同理

技術分享圖片

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<string>
 7 #include<queue>
 8 #include<map>
 9 using namespace std;
10 const int N=1e7;
11 char s[N];
12 int len,ne[N],j,k;
13 int main()
14 {
15     while(scanf("%d",&len)!=EOF)
16     {
17         if(!len) break;
18         scanf("%s",s+1);k++;
19         printf("Test case #%d\n",k);
20         for(int i=0;i<=len;++i) ne[i]=0;
21         for(int i=2;i<=len;++i)
22         {
23             j=ne[i-1];
24             while(j && s[i]!=s[j+1]) j=ne[j];
25             if(s[i]==s[j+1]) ne[i]=j+1;
26         }
27         for(int i=1;i<=len;++i)
28         if(i%(i-ne[i])==0 && i/(i-ne[i])>1) 
29          printf("%d %d\n",i,i/(i-ne[i]));
30         printf("\n");
31     }
32     return 0;
33 }
View Code

poj 1961 Period