HDU 2609 How many

解题思路:最大or最小表示法找出最大or最小,然后重组字符串,用set去重,看set.size()即可

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <string>
 5 #include <set>
 6 using namespace std;
 7 
 8 int Len;
 9 set<string> result;
10 
11 int FindMin(string a)
12 {
13     int i = 0, j = 1, k = 0;
14     while (i < Len && j < Len && k < Len)
15     {
16         if (a[(i + k) % Len] == a[(j + k) % Len])
17             k++;
18         else if (a[(i + k) % Len] > a[(j + k) % Len])
19         {
20             i = i + k + 1;
21             k = 0;
22         }
23         else
24         {
25             j = j + k + 1;
26             k = 0;
27         }
28         if (i == j)
29             j++;
30     }
31 
32     return min(i, j);
33 }
34 
35 int main()
36 {
37     ios::sync_with_stdio(false);
38     int cas;
39     while (cin >> cas)
40     {
41         for (int i = 0; i < cas; ++i)
42         {
43             string s, temp;
44             cin >> s;
45             Len = s.size();
46             int k = FindMin(s);
47             for (int j = k; j < Len; ++j)
48                 temp += s[j];
49             for (int j = 0; j < k; ++j)
50                 temp += s[j];
51             result.insert(temp);
52         }
53 
54         cout << result.size() << endl;
55         result.clear();
56     }
57 
58     return 0;
59 }

 

posted @ 2018-06-01 14:59  duck_lu  阅读(130)  评论(0编辑  收藏  举报