题目传送门

题目大意:给你n个雪花,每个雪花的六个棱都有各自的长度,如果存在两片雪花的每条棱长度对应相同,则输出一句英文,如果不存在就输出另外一句英文,n和长度都比较大。

思路:第一次真正接触哈希,查了题解,还没能通过这道题get到哈希的精髓。这道题是把雪花的六个长度加起来取模的值作为哈希值,然后把拥有相同哈希值的雪花归为一类,每一次输入新的雪花,就先找到它的那一类,然后这一类中进行遍历,遍历的方式也就是在一个环里面暴力,我对这道题用哈希的理解是,利用哈希值进行分类(和并查集有点像),在类中寻找,减少遍历的次数。

代码有详细注释。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int mod = 999983;
const int maxn = 100005;
int tot = 0,n,head[mod+5],v[maxn][6],next[maxn]; int gethash(int id) { //得到哈希值 每位都加起来取余
int hashval=0;
for(int i=0; i<6; i++) {
hashval=(hashval%mod+v[id][i]%mod)%mod;
}
return hashval;
}
void addvhead(int hashval) { //邻接表
next[tot]=head[hashval];//head表示 六位数总和为 hashval的最后一个元素的标号 next[i]表示i的上一个表示是什么
head[hashval]=tot++;//更新head tot就是当前加入的id
}
bool compare(int id,int x) {
for(int i=0; i<6; i++) { //顺时针 比较
int f=0;
for(int st=i,j=0; j<6; j++,st= st+1 >= 6 ? 0 : st+1) {
if(v[id][st]==v[x][j])f++;
else break;
}
if(f==6)return true;
}
for(int i=0; i<6; i++) { //逆时针
int f=0;
for(int st=i,j=5; j>=0; j--,st= st+1 >= 6 ? 0 : st+1) {
if(v[id][st]==v[x][j])f++;
else break;
}
if(f==6)return true;
}
return false;
}
bool pd(int id) {
int hashval=gethash(id);
for(int i=head[hashval]; i!=-1; i=next[i]) { //遍历哈希值相同的雪花
if(compare(id,i))return true;
}
addvhead(hashval); return false;
}
int main() {
memset(head,-1,sizeof(head));
cin>>n;
bool flag=false;
for(int i=0; i<n; i++) {
for(int j=0; j<6; j++)scanf("%d",&v[i][j]);
if(flag)continue;
if(pd(i))flag=true; }
if(flag)cout<<"Twin snowflakes found."<<endl;
else cout<<"No two snowflakes are alike."<<endl;
}
Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 47318   Accepted: 12345

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by nlines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

poj3349找相同的雪花(哈希)的更多相关文章

  1. poj3274 找平衡数列(哈希加一点数学思维)

    题目传送门 题目大意:有n只牛,每只牛有k个属性,接下来n个数字,每个数字的二进制位上的1和0分别表示某种属性的有或者无,然后一个特殊数列就是,一个区间内所有牛的各种属性的总和相等(有e种1属性  e ...

  2. POJ3349 Snowflake Snow Snowflakes 【哈希表】

    题目 很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组 题解 hash表入门题 先把一个六元组的积 + 和取模作为hash值,然后查表即可 期望\(O(n)\) #inc ...

  3. Junit 注解 类加载器 .动态代理 jdbc 连接池 DButils 事务 Arraylist Linklist hashset 异常 哈希表的数据结构,存储过程 Map Object String Stringbufere File类 文件过滤器_原理分析 flush方法和close方法 序列号冲突问题

    Junit 注解 3).其它注意事项: 1).@Test运行的方法,不能有形参: 2).@Test运行的方法,不能有返回值: 3).@Test运行的方法,不能是静态方法: 4).在一个类中,可以同时定 ...

  4. Python 字典和集合基于哈希表实现

    哈希表作为基础数据结构我不多说,有兴趣的可以百度,或者等我出一篇博客来细谈哈希表.我这里就简单讲讲:哈希表不过就是一个定长数组,元素找位置,遇到哈希冲突则利用 hash 算法解决找另一个位置,如果数组 ...

  5. 【LeetCode】389.找不同

    389.找不同 知识点:哈希表.抵消思想: 题目描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. ...

  6. [CareerCup] 9.2 Robot Moving 机器人移动

    9.2 Imagine a robot sitting on the upper left corner of an X by Y grid. The robot can only move in t ...

  7. 海量数据集利用Minhash寻找相似的集合【推荐优化】

    MinHash 首先它是一种基于 Jaccard Index 相似度的算法,也是一种 LSH 的降维的方法,应用于大数据集的相似度检索.推荐系统.下边按我的理解介绍下MinHash 问题背景 给出N个 ...

  8. HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二

    题意:    起初定28张卡牌的排列,把其中11,  21, 31, 41移动到第一列,然后就出现四个空白,每个空白可以用它的前面一个数的下一个数填充,例如43后面的空格可以用44填充,但是47后面即 ...

  9. 关系型数据库工作原理-数据结构(翻译自Coding-Geek文章)

    本文翻译自Coding-Geek文章:< How does a relational database work>. 原文链接:http://coding-geek.com/how-dat ...

随机推荐

  1. MySQL存储引擎 -- MyISAM(表锁定) 与 InnoDB(行锁定) 锁定机制

    前言 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核心技术之一.本章将对MySQL中两种 ...

  2. 关于handler的再次讨论

    主要有两个问题,post方法和sendmessage方法有什么不同? 同一个handler对象发送的message只能发送给自己吗? 问题1: post方法,对于Handler的Post方式来说,它会 ...

  3. mock SpringMVC 测试控制器方法

    从Spring3.2开始 Spring包含了一种mockSpringMVC并针对controller执行http请求的机制 如(该代码选自spring实战4): public void shouldS ...

  4. php环境引起的"syntax error unexpected $end"

    今天在网上淘了一段php和javascript的二级联动下拉菜单的代码.本地运行的时候,一直提示错误 syntax error unexpected $end 本来还以为是括号或者标签没有用好,但是仔 ...

  5. 用fontcreator创建了一个半成品的字体

    下效果,哈哈. 为啥说半成品呢?因为只制作了0到9这几个字符,其他的字母.汉字.符号啥的都没有制作,唯一感觉就是字体设计是一个非常有设计感的活儿,而且需要付出很多的精力,尤其是汉字字体,常见的有6k多 ...

  6. ZROI2018提高day1t1

    传送门 分析 在考场上我通过画图发现了对于n个点肯定用一个六边形围起来最优(假装四边形是特殊的六边形),我们发现可以将这个六边形分成两个梯形(梯形的高可以为0),然后我们便枚举两个梯形共同的底边和它们 ...

  7. Mat表达式

    利用C++中的运算符重载,Opencv2中引入了Mat运算表达式.这一新特点使得使用c++进行编程时,就如同写Matlab脚本. 例如: 如果矩阵A和B大小相同,则可以使用如下表达式: C=A+B+1 ...

  8. C++面试笔记--单链表

    1.编程实现单链表删除节点.       解析:如果删除的是头节点,如下图: 则把head指针指向头节点的下一个节点.同时free p1,如下图所示: 如果删除的是中间节点,如下图所示: 则用p2的n ...

  9. CodeForces 384E Propagating tree (线段树+dfs)

    题意:题意很简单么,给定n个点,m个询问的无向树(1为根),每个点的权值,有两种操作, 第一种:1 x v,表示把 x 结点加上v,然后把 x 的的子结点加上 -v,再把 x 的子结点的子结点加上 - ...

  10. Android ExpandableListView的使用

    一.MainActivity要继承ExpandableListActivity.效果是当单击ListView的子项是显示另一个ListView. package com.example.explear ...