码迷,mamicode.com
首页 > 编程语言 > 详细

poj1961Period【kmp next数组】

时间:2015-08-26 21:57:57      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

大意:跟poj2406一样的题  思路见http://www.cnblogs.com/zhanzhao/p/4761477.html

代码:

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 1000005;
 7 
 8 int next[maxn];
 9 
10 void get(char *s) {
11     int l = strlen(s);
12     int j = 0, k = -1;
13     next[0] = -1;
14     while(j < l) {
15         if(k == -1 || s[j] == s[k]) {
16             
17 
18 
19 //            if(s[++j] == s[++k]) {
20 //                next[j] = next[k];
21 //            } else {
22 //                next[j] = k;
23 //            }
24             next[++j] = ++k;
25         } else {
26             k = next[k];
27         }
28     }
29 }
30 char s[maxn];
31 
32 int main() {
33     int n;
34     int kase = 1;
35     while(scanf("%d",&n) && n) {
36         scanf("%s",s);
37         printf("Test case #%d\n", kase++);
38         get(s);
39         int l = strlen(s);
40 //        for(int i = 0; i <= l; i++) {
41 //            printf("%d ", next[i]);
42 //        }puts("");
43         for(int i = 2; i <= l; i++) {
44             int ans = 1;
45             if(i % (i - next[i]) == 0) {
46                 ans = i / (i - next[i]);
47             }
48             if(ans > 1) {
49                 printf("%d %d\n", i, ans);
50             }
51         }
52         puts("");
53     }
54 }
View Code

 

poj1961Period【kmp next数组】

标签:

原文地址:http://www.cnblogs.com/zhanzhao/p/4761549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!