Period
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 19817 | Accepted: 9640 |
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 A
K ,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.
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
Source
简单的对next数组的应用,
http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html,看这个看明白了如何求循环节长度吧,最大重复次数,
1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <deque> 8 #include <vector> 9 #include <set> 10 #include <algorithm> 11 #include <math.h> 12 #include <cmath> 13 #include <stack> 14 #include <iomanip> 15 #define mem0(a) memset(a,0,sizeof(a)) 16 #define meminf(a) memset(a,0x3f,sizeof(a)) 17 #define ll long long 18 using namespace std; 19 int nex[1000005]; 20 int getn(int n,char c[]) 21 { 22 int i=0,j=-1; 23 nex[0]=-1; 24 while(i<n) 25 { 26 if(c[i]==c[j]||j==-1) 27 { 28 i++;j++;nex[i]=j; 29 } 30 else j=nex[j]; 31 } 32 } 33 int main() 34 { 35 int n,k=1; 36 char c[1000005]; 37 int count=1; 38 while(scanf("%d",&n)&&n) 39 { 40 scanf("%s",c); 41 getn(n,c); 42 printf("Test case #%d\n",count++); 43 for(int i=0;i<=n;++i) cout<<"nec[i]="<<nex[i]<<endl; 44 for(int i=1;i<=n;++i) 45 { 46 int temp=i-nex[i]; 47 if(i%temp==0&&i/temp>1) printf("%d %d\n",i,i/temp); 48 } 49 printf("\n"); 50 } 51 return 0; 52 }
时间并不会因为你的迷茫和迟疑而停留,就在你看这篇文章的同时,不知道有多少人在冥思苦想,在为算法废寝忘食,不知道有多少人在狂热地拍着代码,不知道又有多少提交一遍又一遍地刷新着OJ的status页面……
没有谁生来就是神牛,而千里之行,始于足下!