标签:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 14657 | Accepted: 6967 |
Description
Input
Output
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
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 char a[1000100]; int lena, p[1000100]; 6 void Getp() 7 { 8 int i = 0, j = -1; 9 p[i] = j; 10 while(i < lena) 11 { 12 if(j == -1 || a[i] == a[j]) 13 { 14 i++; j++; 15 p[i] = j; 16 } 17 else 18 j = p[j]; 19 } 20 } 21 int main() 22 { 23 int t, csse = 1;; 24 while(~scanf("%d", &t), t) 25 { 26 scanf("%s", a); 27 lena = strlen(a); 28 Getp(); 29 printf("Test case #%d\n", csse++); 30 for(int i = 1; i <= lena; i++) 31 { 32 //int length = i - p[i]; //循环节长度; 33 if(i % (i - p[i]) == 0 && p[i] != 0) //判断是否为循环节; 34 printf("%d %d\n", i, i/(i-p[i])); 35 } 36 printf("\n"); 37 } 38 return 0; 39 }
Poj1961--Period(Kmp, Next数组求循环节长度 && 出现次数)
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4713364.html