题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2609
题目大意:
有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状的结构,求可以经过循环旋转,最后不同的串有多少个。。
解题思路:
将所有字符串用最小表示法表示,然后存入set判重
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
set<string>tot;
string change_min(char s[])
{
int n = strlen(s);
int i = , j = , k = ;
while(i < n && j < n && k < n)
{
int t = s[(i + k) % n] - s[(j + k) % n];
if(!t)
k++;
else
{
if(t > )
i += k + ;
else
j += k + ;
if(i == j)j++;
k = ;
}
}
int ans = i < j ? i : j;
string cnt;
for(int i = ans; i < ans + n; i++)
{
cnt += s[i % n];
}
return cnt;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
char s[];
tot.clear();
string mins;
for(int i = ; i < n; i++)
{
scanf("%s", s);
mins = change_min(s);
//cout<<mins<<endl;
tot.insert(mins);
}
cout<<tot.size()<<endl;
}
return ;
}