hdu-2609 How many---最小表示法模板+set判重

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2609

题目大意:

有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状的结构,求可以经过循环旋转,最后不同的串有多少个。。

解题思路:

将所有字符串用最小表示法表示,然后存入set判重

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<string>
 6 #include<set>
 7 using namespace std;
 8 const int maxn = 100000 + 10;
 9 const int INF = 0x3f3f3f3f;
10 set<string>tot;
11 string change_min(char s[])
12 {
13     int n = strlen(s);
14     int i = 0, j = 1, k = 0;
15     while(i < n && j < n && k < n)
16     {
17         int t = s[(i + k) % n] - s[(j + k) % n];
18         if(!t)
19             k++;
20         else
21         {
22             if(t > 0)
23                 i += k + 1;
24             else
25                 j += k + 1;
26             if(i == j)j++;
27             k = 0;
28         }
29     }
30     int ans = i < j ? i : j;
31     string cnt;
32     for(int i = ans; i < ans + n; i++)
33     {
34         cnt += s[i % n];
35     }
36     return cnt;
37 }
38 int main()
39 {
40     int n;
41     while(scanf("%d", &n) != EOF)
42     {
43         char s[105];
44         tot.clear();
45         string mins;
46         for(int i = 0; i < n; i++)
47         {
48             scanf("%s", s);
49             mins = change_min(s);
50             //cout<<mins<<endl;
51             tot.insert(mins);
52         }
53         cout<<tot.size()<<endl;
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/fzl194/p/8934083.html