Periodtime limit: 3000 msmemory limit: 30000 kbthis problem will be judged on PKU. Original ID: 1961
64-bit integer Io format: % LLD Java class name: Main 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. of course, we also want to know the period K. inputthe 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
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
Sourcesoutheastern Europe 2004: KMP. Evaluate the information of the periodic string in the string prefix, the length of the output prefix string, and the number of cycles.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1000010;18 char str[maxn];19 int n,fail[maxn],cs = 1;20 void getFail(){21 fail[0] = fail[1] = 0;22 int i;23 printf("Test case #%d\n",cs++);24 for(i = 1; str[i]; i++){25 int j = fail[i];26 if(i%(i-fail[i]) == 0 && i/(i - fail[i]) > 1)27 printf("%d %d\n",i,i/(i-fail[i]));28 while(j && str[i] != str[j]) j = fail[j];29 fail[i+1] = str[i] == str[j] ? j+1:0;30 }31 if(i%(i - fail[i]) == 0 && i/(i - fail[i]) > 1)32 printf("%d %d\n",i,i/(i - fail[i]));33 }34 int main() {35 bool ok = false;36 while(scanf("%d",&n),n){37 if(ok) puts("");38 ok = true;39 scanf("%s",str);40 getFail();41 }42 return 0;43 }
View code
Poj 1961 Period