POJ 1961 循环节

和POJ 2406 几乎一样。前者是求 该字符串的最小的循环节。也就是最大的循环次数。后者是求该字符串的每个前缀的循环节的最大循环次数。(如果有的话)。而且必须大于1。才可以输出。就是POJ 2406变形。加一个循环遍历就可以了。当然了。结论仍然是我【记住】的。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 #define maxn 1000010
 7 int next[maxn];
 8 char str[maxn];
 9 
10 void getNext(char str[]) {
11     int len = strlen(str+1);
12     next[1] = 0;
13     for (int k=0, q=2; q<=len; ++q) {
14         while(k>0 && str[k+1]!=str[q])
15             k = next[k];
16         if (str[k+1] == str[q])
17             k++;
18         next[q]=k;
19     }
20 }
21 
22 int main() {
23     int len;
24     int cnt = 0;
25     while(cin >> len && len) {
26         cin >> (str+1);
27         getNext(str);
28         cout << "Test case #" << ++cnt << endl;
29         for (int i=2; i<=len; ++i) {
30             int temp = i-next[i];
31             if (temp != i && i % temp == 0) {
32                 cout << i << ' ' << (i/temp) << endl;
33             }
34         }
35         cout << endl;
36     }
37     return 0;
38 }
View Code
原文地址:https://www.cnblogs.com/icode-girl/p/4863168.html