1. 程式人生 > >HDU 3746 Cyclic Nacklace

HDU 3746 Cyclic Nacklace

cst 三種 nbsp isp ++i 循環節 pac AI 圖片

題意:給你幾組字符串,每組添加多少個字符能夠構成循環.

題解:最小循環節,註意討論的三種情況,題上剛好給了這三種情況(要是不給我這弱雞又考慮不全了)

技術分享圖片
 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

HDU 3746 Cyclic Nacklace