POJ 1961 Period(KMP)
和POJ2406差不多,不过用暴力去找,会超时。
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #define N 1000001 5 char str[N]; 6 int next[N]; 7 int main() 8 { 9 int i,j,len,d,a = 0; 10 while(scanf("%d%*c",&len)!=EOF) 11 { 12 a ++; 13 if(len == 0)break; 14 scanf("%s",str); 15 memset(next,0,sizeof(next)); 16 if(str[0] == '.')break; 17 next[0] = -1; 18 j = -1; 19 for(i = 1; i <= len-1; i ++) 20 { 21 while(j >= 0&&str[j+1] != str[i]) 22 j = next[j]; 23 if(str[j+1] == str[i]) j++; 24 next[i] = j; 25 } 26 printf("Test case #%d\n",a); 27 for(j = 1; j <= len; j ++) 28 { 29 d = j-1-next[j-1]; 30 if(j % d == 0&&j/d != 1) 31 printf("%d %d\n",j,j/d); 32 } 33 printf("\n"); 34 } 35 return 0; 36 }