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();
}
}
随机推荐
- E20170626-gg
occupy vt. 占领; 使用,住在…; 使从事,使忙碌; 任职; stack n. 垛,干草堆; (一排) 烟囱; 层积; 整个的藏书架排列;
- .net 必看书籍2
一.入门 1.<HTML与CSS入门经典(第7版) >HTML入门 点评:html语言的入门,由于html极其简单所以同类其他书也可代替,本书并非经典,本书摆在这里纯属占位!你可以用其他书 ...
- 题解报告:hdu 1863 畅通工程
Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可 ...
- EF--ModelFirst
EF框架有三种基本的方式:DB First,Model First,Code First.这里简单的说一下Model First,适合没有基础的同学照着做,学习基础的东西. 1.建立一个类库项目,这个 ...
- gerrit项目配置
1. 相关约定说明: 1.1 gerrit服务器ip地址:192.168.130.10 1.2 gerrit服务器端用户名:gerrit 1.3 gerrit用户端管理员:admin 1.4 ssh端 ...
- 为什么现在改用int.TryParse了
以前一直用 int.Parse(x)或者 Convert.ToInt64(x),后来项目中发现如果x变量的值为null是,就报错了,哪怕我这样写 int.Parse(x=x??"0" ...
- 从源码看ASP.NET框架(一)-打造页面控件树
测试实例如下: 前台代码MyFirstWeb.aspx(没有服务器控件,即没有runat) CodeBehind="MyFirstWeb.aspx.cs":表示代码后置类文件 In ...
- Python学习(一)-在VS上搭建开发环境
1.到官网下载最新Python 注意:虽然目前大部分应用是Python2写的,但Python3必定会成为以后的主流 不管选择学习哪个,了解pyhton2和pyhton3的差异是必须的 2.安装Pyth ...
- 在ubuntu系统下下载和卸载skype
1.下载 sudo apt-get install skypeforlinux 2.卸载 sudo apt remove skypeforlinux
- [转]Wote用python语言写的imgHash.py
#!/usr/bin/python import glob import os import sys from PIL import Image EXTS = 'jpg', 'jpeg', 'JPG' ...