P - How many
2024-08-28 04:49:30
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me
How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.
How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.
InputThe input contains multiple test cases.
Each test case include: first one integers n. (2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include '0','1').
OutputFor each test case output a integer , how many different necklaces.Sample Input
4
0110
1100
1001
0011
4
1010
0101
1000
0001
Sample Output
1
2
#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 10002
/*
题目相当于求所有字符串中
能通过相互循环位移得到的字符串数目
对每个字符串求最小表示法,然后加入到set中
*/
string str;
set<string> S;
int GetMin(string s,int len)
{
int i=,j=,k=;
while(i<len&&j<len&&k<len)
{
if(s[(i+k)%len]==s[(j+k)%len])
k++;
else if(s[(i+k)%len]>s[(j+k)%len])
{
i = i+k+;
k = ;
}
else
{
j = j+k+;
k = ;
}
if(i==j)
j++;
}
return min(i,j);
}
int main()
{
int n;
string tmp;
tmp.reserve();
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
{
tmp.clear();
cin>>str;
int pos = GetMin(str,str.size()),L = str.size();
for(int j=pos,cnt=;cnt<L;j=(j+)%L,cnt++)
{
tmp.push_back(str[j]);
}
S.insert(tmp);
}
cout<<S.size()<<endl;
S.clear();
}
}
最新文章
- de4dot3.14更新文件打包下载
- mysql的故事
- mysql在ubuntu下的安装
- Linux常用压缩和解压命令
- Wikipedia : OIT history
- Debug的F5~F8用法
- MSP430F149学习之路——按键与LED
- Ubuntu重启网络/etc/init.d/networking restart报错
- Windows Forms(二)
- boost 的函数式编程库 Phoenix入门学习
- SpringMVC第一天(其他)
- C# 6 与 .NET Core 1.0 高级编程 - 40 ASP.NET Core(上)
- 数值函数ROUND(四舍五入),TRUNC(不四舍五入),MOD
- RobotFramework自动化测试框架的基础关键字(二)
- 为label或者textView添加placeHolder
- java跨域
- 对于长沙互联网发展,一个外来两年Java程序员的所见所感所愿
- java的Map遍历
- MFC连接MySQL
- 预加载与智能预加载(iOS)