Period
Time Limit: 3000MS |
|
Memory Limit: 30000K |
Total Submissions: 14653 |
|
Accepted: 6965 |
Description
For each prefix of a given string S with N characters (each character have an ASCII code between and 126, inclusive), we Want to know whether the prefix is a periodic string. That's, for each I (2 <= i <= N) we want to know the largest K > 1 (if there are one) such that the prefix of S W ith length I can be written as AK, which 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 lines. The first one contains n (2 <= n <= 1 000 000), haven size of the string s.the second line contains the string S. The Input file ends with a line, have the
Number zero on it.
Output
For each test case, output ' test Case # ' and the consecutive test case number on a '; Then, for each prefix with length I that have a period K > 1, output the prefix size I and the period K separated by a s Ingle Space; The prefix sizes must is in increasing order. Print a blank line after each test case.
Sample Input
3aaa12aabaabaabaab0
Sample Output
Test Case #12 3Test case #22 26 29 312 4
Test instructions: The length of the prefix, and the number of cycles in which the smallest loop section should be, but the number of cycles must be greater than 1
#include <stdio.h> #include <string.h> #define MAX 1000100int Next[max];char str[max];int a[max],b[max];int N,m,k;void Getfail () {int i,j;next[0]=next[1]=0;for (i=1;i<n;i++) {j=next[i];while (J&&str[i]!=str[j]) j=next[j];next[i+1]=str[i]==str[j]?j+1:0;}} void KMP () {int i,j;for (i=1;i<=n;i++) {k=i;if (k== (K-next[k]))//k-next[k] min. cycle section length Continue;if (k% (k-next[k)) ==0) printf ("%d%d\n", i,k/(K-next[k]));} printf ("\ n");} int main () {int i,t=1;while (scanf ("%d", &n), N) {GetChar (); for (i=0;i<n;i++) scanf ("%c", &str[i]); Getfail (); printf ("Test case #%d\n", t++); KMP ();} return 0;}
POJ 1961 Period "to find the length of the prefix and the number of cycles in which the smallest loop section is"