HDU 3746 Cyclic Nacklace

题意:给你几组字符串,每组添加多少个字符能够构成循环.

题解:最小循环节,注意讨论的三种情况,题上刚好给了这三种情况(要是不给我这弱鸡又考虑不全了)

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

 

posted @ 2018-04-23 21:49  duck_lu  阅读(91)  评论(0编辑  收藏  举报