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>(是不是比喻反了) 我们要设计一个函数,这个函数要确保同一个东西能得到相同的函数值( ...
随机推荐
- 散货:null 强转 和 iOS null崩溃
问题1 在看 SpringMVC源码剖析(五)-消息转换器HttpMessageConverter 的时候,在 org.springframework.web.servlet.mvc.method ...
- dom元素改变监听
function domChange(domId, callback) { // select the target node var target = document.getElementById ...
- jquery-插入兄弟元素
1.after方法 在匹配元素集合中的每个元素的 后面 插入参数所指定的内容,作为其兄弟节点 参数类型说明: 1)普通字符串(可包含各种html标签) $('div').after('html字符串' ...
- javascript 哈夫曼树构造
function Node(data) { this.data = data; this.left = null; this.right = null; } Array.prototype.creat ...
- Ubuntu下 Oracle sqldeveloper中文目录、文件,select查询结果中:中文乱码
是由于JDK所致.下面是网上的解决方案 解决案例1: .0_24/jre/lib/fonts.进入到fonts目录,新建文件夹 fallback cd /usr/java/jdk1..0_24/jre ...
- DEDECMS教程:首页实现分页的两种方法
有两种办法可以实现: 一.用arclist标签+Ajax实现织梦首页分页 二.交叉栏目ID 实现织梦首页分页 一.用arclist标签+Ajax实现织梦首页分页 1.必须在首页<head> ...
- 我的javascript心跳机
li { list-style: none!important; padding:0; } .list_num{ list-style-type:decimal; } .list_inline{ ma ...
- 关于直播学习笔记-004-nginx-rtmp、srs、vlc、obs
1.采集端:OBS RTMP推流地址:rtmp://192.168.198.21:1935/live 流密钥:livestream(任意-但播放地址与此一致) 2.播放端:nginx-rtmp-win ...
- GIS-012-ArcGIS JS API 绘图
Name Description ARROW Draws an arrow. CIRCLE Draws a circle. DOWN_ARROW Draws an arrow that points ...
- PHP-006
Q:应用程序执行时的路径 "D:\*******\protected\runtime" 是无效的. 请确定它是一个可被 Web server process 写入资料的目录. A: ...