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. 1分钟试用PowerShell 5.0新功能PowerShellGet安装Script Browser和Script Analyzer

    微软PowerShell 产品组上周发布了PowerShell 5.0 PowerShellGet功能.有了它,IT 人员可以方便地搜索,安装,更新PowerShell Module.在这篇博客中,我 ...

  2. python3下载图片

    import urllib.request import socket import re import sys import os targetDir = r"E:\\DATA\常用py脚 ...

  3. tar 的–exclude参数,实现不包括某些文件(转)

    最近有人问我tar压缩能不能不包含不想要的文件或者文件夹 压缩包很利于传送,备份等等.但比如大量的日志文件一般是不需要备份,或者需要分开备份. tar参数中的–exclude,这个是我在看鸟哥私房菜时 ...

  4. [svc]inotify+rsync解决nfs单点问题

    安装配置inotify 参考 yum install inotify* -y [root@n2 shell]# rpm -qa|grep inotify inotify-tools-3.14-8.el ...

  5. 6-9-哈夫曼树(HuffmanTree)-树和二叉树-第6章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第6章  树和二叉树 - 哈夫曼树(HuffmanTree) ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

  6. JAVA 线程池架构浅析

    经历了Java内存模型.JUC基础之AQS.CAS.Lock.并发工具类.并发容器.阻塞队列.atomic类后,我们开始JUC的最后一部分:线程池.在这个部分你将了解到下面几个部分: 线程池的基础架构 ...

  7. Python(十)之GUI编程

    在windwos上实现:使用wxPython模块 #!/usr/bin/env python # encoding: utf-8 import wx #创建open按钮触发的事件对应的函数 def l ...

  8. 【iCore1S 双核心板_FPGA】例程八:触发器实验——触发器的使用

    实验现象: 在本实验中,将工程中的D触发器.JK触发器实例化,对应其真值表,用signal对其进行 检验,利用SignaTap II观察分析波形. 核心代码: module D( input CLK, ...

  9. JavaScript高级用法二之内置对象

    综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 什么是对象 2 Date 日期对象 3 返回/设置年份方法 4 返回星期方法 5 返回/设置时间方法 6 String 字符串对象 7 返回 ...

  10. PLSQL存储过程(基础篇)-转

    我不是专门的开发人员,但存储过程又是很重要的知识,为了能够很好的记忆,现把这些基础知识总结一下.存储过程可以实现代码的充分共享,提高系统性能. 基础篇       知识回顾 如果经常使用特定操作,哪么 ...