How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4210    Accepted Submission(s): 1936

Problem Description
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.
 
Input
The 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').
 
Output
For 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
 
Author
yifenfei
 
Source
 
Recommend
yifenfei
 

题意:

给定n个01串,统计一共有多少种不同的。如果他们的循环同构是相同的那他们就是相同的。

思路:

刚开始很暴力的把每个串的所有循环同构串都丢进set里,直接MLE了。太暴力了。

应该要想到如果串相同的话,他们的最小表示法肯定是一样的。所以只需要存最小表示法就可以了。

然后也不需要统计,直接print set的大小就可以了。

每输入一个数,找到他的最小表示法。

然后对这个最小表示法进行hash,结果丢进set

处理完所有字符串,print set的大小。

 #include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
//#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e4 + ;
int n;
char str[ * ];
ull h[];
set<ull>types; int main()
{
while(scanf("%d", &n) != EOF){
types.clear();
for(int i = ; i < n; i++){
scanf("%s", str + );
int len = strlen(str + );
for(int j = ; j <= len; j++)str[len + j] = str[j];
int j = , k = , p;
while(j <= len && k <= len){
for(p = ; p < n && str[j + p] == str[k + p];p++);
if(p == len)break;
if(str[j + p] > str[k + p]){
j = j + p + ;
if(j == k)j++;
}
else{
k = k + p + ;
if(j == k)k++;
}
}
int pos = min(j, k); for(j = ; j <= len; j++){
h[j] = h[j - ] * + str[j + pos - ] - '';
}
types.insert(h[len]);
}
printf("%d\n", types.size());
}
return ;
}

hdu2609 How many【最小表示法】【Hash】的更多相关文章

  1. POJ 1635 树的最小表示法/HASH

    题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...

  2. HDU2609 How many —— 最小表示法

    题目链接:https://vjudge.net/problem/HDU-2609 How many Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  3. 1282 时钟(最小表示法+hash)

    1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分整 ...

  4. luogu P5043 【模板】树同构 hash 最小表示法

    LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...

  5. hdu2609 How many 字典树+最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow many ...

  6. hdu2609 最小表示法

    Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...

  7. poj 3349 (最小表示法)

    开始按hash做的 交上去就wa 但是和标称拍了半天也没有不一样的 可能是生成的数据太水了吧... #include<iostream> #include<cstdio> #i ...

  8. [BZOJ4337][BJOI2015]树的同构(树的最小表示法)

    4337: BJOI2015 树的同构 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1023  Solved: 436[Submit][Status ...

  9. Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划

    亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...

随机推荐

  1. 【NIO】Java NIO之通道

    一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...

  2. CucumberPeople 1.3.2 发布

    CucumberPeople 网站: http://alterhu.github.io/CucumberPeople/ This eclipse plugin based on RubyMine ,a ...

  3. 卸载系统自动jdk

    执行下面的代码可以看到当前各种JDK版本和配置: sudo update-alternatives --config java   卸载系统自动jdk [root@localhost soft]# r ...

  4. MySQL 聚簇索引&&二级索引&&辅助索引

    MySQL非聚簇索引&&二级索引&&辅助索引 mysql中每个表都有一个聚簇索引(clustered index ),除此之外的表上的每个非聚簇索引都是二级索引,又叫辅 ...

  5. java webdriver的api的封装

    我们来看一下官网提供的代码写法,即最原始的写法: driver.findElement(By.id("kw")).click() 这样写是没任何问题的,但这样没有把元素对象,数据, ...

  6. 第三方文本框 在div中显示预览,让指定节点不受外部css影响

    例如,富文本框中 ol  li 但是我们往往全局样式时候会 让前面的数字不显示,但是富文本框时候,录入,我们需要显示,但是div中就不显示了 我们在预览页面中加上一个指定样式   然后后面 加上!im ...

  7. PHP —— 识别运算符实现逻辑比较

    最近遇到一个功能的开发,大致意思就是根据用户输入的条件,进行相关的比较操作.本来打算使用用户选择运算符的方式,但是后来结合项目实际,发现需要使用用户输入的自定义运算比较现实一点.大致意思就是: 1.用 ...

  8. 如何用javac 和java 编译运行整个Java工程

    转自:http://blog.csdn.net/huagong_adu/article/details/6929817      前言:本文教你怎么用javac和Java命令,以及如何利用脚本(she ...

  9. Egret里用矢量挖圆形的洞

    项目里需要用到,但是不是用在新手引导上,下面的代码可以绘制一个圆的四分之一,用四个即可拼出一个圆. private createShape(): egret.Shape { let magicNum ...

  10. 《转》vue 常用ui组件

    vux github ui demo:https://github.com/airyland/vux Mint UI 项目主页:http://mint-ui.github.io/#!/zh-cndem ...