hdu2609 How many【最小表示法】【Hash】
How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4210 Accepted Submission(s): 1936
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.
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').
0110
1100
1001
0011
4
1010
0101
1000
0001
2
题意:
给定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】的更多相关文章
- POJ 1635 树的最小表示法/HASH
题目链接:http://poj.org/problem?id=1635 题意:给定两个由01组成的串,0代表远离根,1代表接近根.相当于每个串对应一个有根的树.然后让你判断2个串构成的树是否是同构的. ...
- HDU2609 How many —— 最小表示法
题目链接:https://vjudge.net/problem/HDU-2609 How many Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- 1282 时钟(最小表示法+hash)
1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形的,P个刻度均分整 ...
- luogu P5043 【模板】树同构 hash 最小表示法
LINK:模板 树同构 题目说的很迷 给了一棵有根树 但是重新标号 言外之意还是一棵无根树 然后要求判断是否重构. 由于时无根的 所以一个比较显然的想法暴力枚举根. 然后做树hash或者树的最小表示法 ...
- hdu2609 How many 字典树+最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell meHow many ...
- hdu2609 最小表示法
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- poj 3349 (最小表示法)
开始按hash做的 交上去就wa 但是和标称拍了半天也没有不一样的 可能是生成的数据太水了吧... #include<iostream> #include<cstdio> #i ...
- [BZOJ4337][BJOI2015]树的同构(树的最小表示法)
4337: BJOI2015 树的同构 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1023 Solved: 436[Submit][Status ...
- Luogu P3886 [JLOI2009]神秘的生物 最小表示法,轮廓线DP,插头DP,动态规划
亲手写掉的第一道最小表示法!哈哈哈太开心啦~ 不同于以往的几个插头\(dp\),这个题目的轮廓线是周围的一圈\(n\)个格子.而其所谓"插头"也变成了相邻格子的所属连通分量编号,并 ...
随机推荐
- 【NIO】Java NIO之缓冲
一.前言 在笔者打算学习Netty框架时,发现很有必要先学习NIO,因此便有了本博文,首先介绍的是NIO中的缓冲. 二.缓冲 2.1 层次结构图 除了布尔类型外,其他基本类型都有相对应的缓冲区类,其继 ...
- ASP.NET CORE中判断是否移动端打开网页
using Microsoft.AspNetCore.Http;using System;using System.Collections.Generic;using System.Text;usin ...
- 译: 1. RabbitMQ Spring AMQP 之 Hello World
本文是译文,原文请访问:http://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html RabbitMQ 是一个Brocker (消息队 ...
- react.js map遍历的问题
React遍历多个Ant Design中的Upload组件时,随意删除任一个Upload出现了bug,依次点击上传图片后,当点击删除时,倒着删除没有问题,从中间和从开头删问题出现了,出现了类似塌方的效 ...
- 9-9-B+树-查找-第9章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第9章 查找 - B+树 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版>(严蔚敏,吴伟民版)课本源码+习题 ...
- 《软件测试自动化之道》读书笔记 之 SQL 存储过程测试
<软件测试自动化之道>读书笔记 之 SQL 存储过程测试 2014-09-28 待测程序测试程序 创建测试用例以及测试结果存储 执行T-SQL脚本 使用BCP工具导入测试用例数据 ...
- C#中怎么判断一个数组中是否存在某个数组值
(1) 第一种方法: ,,}; ); // 这里的1就是你要查找的值 ) // 不存在 else // 存在 (2) 第二种方法: string[] strArr = {"a",& ...
- OAuth 2.0 C# 版
using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using Syste ...
- Netbeans rcp中获得本地文件系统路径
通过file协议 —————————————————————————————————————————————————————— URL url = new URL("file:///E:/A ...
- MUI 打包android app
自有证书生成方法 manifest配置 (2).图标配置:所有图片格式必须是png,且严格符合分辨率要求.使用其他图片格式重命名为png会导致打包失败.配置图标时选择自动生成所有适用图标,选择一个大图 ...