P - How many
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();
}
}
随机推荐
- 2-1 创建第一个Vue实例
https://cn.vuejs.org/ 生产版本是经过压缩后的版本,它里面把一些没用的警告的代码删除掉了所以它会更精悍一些. 直接引入CDN的网址我们就可以使用Vuejs了.使用生产版本或者CDN ...
- vs打开wixproj后缀文件
1.在正常情况下vs是无法打开wixproj工程的,能打开也只能是以记事本方式打开该文件本身 2.所以此时需要下载wixtool,安装后即可打开上述类型文件 3.最好也安装好vs对应版本的扩展包 4. ...
- Behavior Designer扩展
BehaviorManager.instance.Tick(behaviorTree); 卸载update里u3d直接卡死 = = SharedVariable直接赋值会改变他的引用关系,必须用XXX ...
- jeecg中列表查询数据关联其他表的显示
1.A表字段:id,name;B表字段:id,name,fid(A表外键),现查询A表和B表的所有数据并且查询条件A,B都有,在前台页面list显示 2.后台方法: @RequestMapping(p ...
- Centos 7 安装google 浏览器(yum 方式)
过程: 1 vim /etc/yum/repo.s/google_chrome.repo 2 添加如下内容: [google-chrome] name=google-chrome ...
- mysql在linux下的安装(5.7版本以后)
1.添加mysql组和mysql用户,用于设置mysql安装目录文件所有者和所属组. ①groupadd mysql ②useradd -r -g mysql mysql 2.将二进制文件解压到指定的 ...
- 51nod 1577 线性基
思路: http://blog.csdn.net/yxuanwkeith/article/details/53524757 //By SiriusRen #include <bits/stdc+ ...
- Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼
Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...
- python--9、并发之多进程应用
multiprocessing模块 想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.Python提供了multiprocessing. ...
- html——细线表格
细线: 1.table表格设置背景色 2.table中设置单元格距离 3.tr标签设置另外一种背景色 <!DOCTYPE html> <html> <head lang= ...