poj3349 Snowflake Snow Snowflakes【HASH】
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 49991 | Accepted: 13040 |
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 n lines, 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.
Source
题意:
一朵雪花有六条边,给n朵雪花,问其中有没有相同的雪花。当两个雪花的边都对应相同时,雪花是相同的。
思路:
,其中P是我们选定的一个较大的质数
这个HASH函数把所有的雪花分成了P类,对于每一朵 雪花,定位到head[
]这个表头所指向的链表。如果该链表中不包含和这朵雪花相同的雪花,就在表头后插入一个新节点(对于一些题目可以在该节点上记录出现了的次数)。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e6 + ;
int snow[maxn][], head[maxn], nxt[maxn];
int n, tot, P = ; int H(int *a)
{
int sum = , mul = ;
for(int i = ; i < ; i++){
sum = (sum + a[i]) % P;
mul = (long long)mul * a[i] % P;
}
return (sum + mul) % P;
} bool eequal(int *a, int *b)
{
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
bool eq = ;
for(int k = ; k < ; k++){
//cout<<a[(i + k) % 6]<<" "<<b[(i + k) % 6]<<endl;
if(a[(i + k) % ] != b[(j + k) % ]) eq = ;
}
if(eq) return ;
eq = ;
for(int k = ; k < ; k++){
if(a[(i + k) % ] != b[(j - k + ) % ])eq = ;
}
if(eq)return ;
}
}
return ;
} bool insertt(int *a)
{
int val = H(a);
//cout<<val<<endl;
for(int i = head[val]; i; i = nxt[i]){
//cout<<2<<endl;
if(eequal(snow[i], a)){
//cout<<1<<endl;
return ;
}
}
++tot;
memcpy(snow[tot], a, * sizeof(int));
nxt[tot] = head[val];
head[val] = tot;
return ;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
//tot = 0;
//memset(head, 0, sizeof(head));
for(int i = ; i <= n; i++){
int a[];
for(int j = ; j < ; j++){
scanf("%d", &a[j]);
}
if(insertt(a)){
printf("Twin snowflakes found.\n");
return ;
}
}
printf("No two snowflakes are alike.\n"); }
poj3349 Snowflake Snow Snowflakes【HASH】的更多相关文章
- POJ3349 Snowflake Snow Snowflakes (hash
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48624 Accep ...
- POJ3349 Snowflake Snow Snowflakes 【哈希表】
题目 很简单,给一堆6元组,可以从任意位置开始往任意方向读,问有没有两个相同的6元组 题解 hash表入门题 先把一个六元组的积 + 和取模作为hash值,然后查表即可 期望\(O(n)\) #inc ...
- [poj3349]Snowflake Snow Snowflakes(hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: ...
- POJ--3349 Snowflake Snow Snowflakes(数字hash)
链接:Snowflake Snow Snowflakes 判断所有的雪花里面有没有相同的 每次把雪花每个角的值进行相加和相乘 之后hash #include<iostream> #incl ...
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- 【POJ3349 Snowflake Snow Snowflakes】【Hash表】
最近在对照省选知识点自己的技能树 今天是Hash 题面 大概是给定有n个6元序列 定义两个序列相等 当两个序列各自从某一个元素开始顺时针或者逆时针旋转排列能得到两个相同的序列 求这n个6元序列中是否有 ...
- Snowflake Snow Snowflakes【Poj3349】
Description You may have heard that no two snowflakes are alike. Your task is to write a program to ...
- POJ3349: Snowflake Snow Snowflakes(hash 表)
考察hash表: 每一个雪花都有各自的6个arm值,如果两个雪花从相同或者不同位置开始顺时针数或者逆时针数可以匹配上,那么这两个雪花就是相等的. 我们采用hash的方法,这样每次查询用时为O(1),总 ...
- poj3349 Snowflake Snow Snowflakes
吼哇! 关于开散列哈希: 哈希就是把xxx对应到一个数字的东西,可以理解成一个map<xxx, int>(是不是比喻反了) 我们要设计一个函数,这个函数要确保同一个东西能得到相同的函数值( ...
随机推荐
- 详解JNDI的lookup资源引用 java:/comp/env
ENC的概念: The application component environment is referred to as the ENC, the enterprise naming c ...
- 奇葩问题:ListView中Item与Item中的Button不能单击问题
android中ListView是一个经常要用到的一个组件,用到该组件时经常会碰到ListView的Item和Item中的Button不能单击的问题. 本人在使用时同样也遇到过这样的情况,共有三种情况 ...
- jQuery-编辑选择结果(添加、筛选、过滤或检测)
编辑选择结果 操作 实例 效果 备注 添加 添加选择器 $("p").add(".a") 添加类名为a的选择器 并不影响源结果集 $(" ...
- 落实制度靠流程<摘自平安50万人的执行力>
落实制度靠流程<摘自平安50万人的执行力> 讲在嘴上的制度是给人听的,写在纸上的制度是给人看的,落实在流程上的制度才是可靠的.制度的执行不能都依赖个人的自觉性. 很多企业都在强调和推行制 ...
- java程序后台报错java.net.SocketException: Too many open files
问题描述: 今天一个同事反映程序有问题,让帮忙查看后台日志,发现后台日志报错的信息如下: java.net.SocketException: Too many open files at java.n ...
- C++ 对象间的赋值与拷贝构造函数
1.对象间的赋值 /***A.h文件***/ #pragma once class A { public: int va; A(void); A(char* name); A(const A& ...
- iOS Xcode制作模板类
转载请注明出处http://blog.csdn.net/uxyheaven/article/details/48419963 为什么要定义模板类 遵守代码规范可以提高代码可读性, 降低后期维护成本. ...
- 报错程序包org.springframework.test.context不存在
在pom.xml文件中找到 加入了依赖,但是maven update 或者Reimport后 启动还是报错 最后使出绝招: 在maven仓库的位置 找到对应的文件夹 更奇怪了 发现明明有jar包啊! ...
- js将字符串转换为数字等类型
1.js提供了parseInt()和parseFloat()两个转换函数. 2.ECMAScript中可用的3种强制类型转换如下: Boolean(value)——把给定的值转换成Boolean型: ...
- 【MySQL】查询时强制区分大小写的方法
MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名重复,又会浪费很多资源.再者,李逵.李鬼的多起来,侦辨起来很困难.要做到这一点,要么在建表时,明确大小写敏感(字段明确大小写敏感) sql ...