HDU - 1358 Period

 1 #include <iostream>
 2 #include <algorithm>
 3 #define MaxSize 1000010
 4 using namespace std;
 5 
 6 char s[MaxSize];
 7 int Next[MaxSize];
 8 int Len;
 9 
10 void GetNext()
11 {
12     int i = 0, j = Next[0] = -1;
13     while (i < Len)
14     {
15         if (j == -1 || s[i] == s[j])
16             Next[++i] = ++j;
17         else
18             j = Next[j];
19     }
20 }
21 
22 int main()
23 {
24     ios::sync_with_stdio(false);
25     int cas = 0;
26     while (true)
27     {
28         cin >> Len;
29         if (Len == 0)
30             break;
31         cin >> s;
32         cas++;
33         GetNext();
34         cout << "Test case #" << cas << endl;
35         for (int i = 1; i <= Len; ++i)
36         {
37             int circle = i - Next[i];
38             if (Next[i] == 0)
39                 continue;
40             if (i % circle == 0)
41                 cout << i << ' ' << i / circle << endl;
42         }
43         cout << endl;
44     }
45 
46     return 0;
47 }

 

posted @ 2018-04-26 23:29  duck_lu  阅读(140)  评论(0编辑  收藏  举报