标签:
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 13949 | Accepted: 6601 |
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<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 using namespace std; 6 7 const int maxn = 1000100; 8 int n; 9 int Next[maxn]; 10 char str[maxn]; 11 12 void KMP(){ 13 int j = 0, k = -1; 14 Next[0] = -1; 15 while (str[j]!=‘\0‘){ 16 if (k == -1 || str[j] == str[k]){ 17 k++; 18 j++; 19 if (j % (j - k) == 0 && j / (j - k) > 1) 20 printf("%d %d\n", j, j / (j - k)); 21 Next[j] = k; 22 } 23 else 24 k = Next[k]; 25 } 26 } 27 28 int main() 29 { 30 int iCase = 0; 31 while (scanf("%d", &n) != EOF&&n!=0){ 32 iCase++; 33 scanf("%s", &str); 34 printf("Test case #%d\n",iCase); 35 KMP(); 36 printf("\n"); 37 } 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/lxzd723/p/4401022.html