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>(是不是比喻反了) 我们要设计一个函数,这个函数要确保同一个东西能得到相同的函数值( ...
随机推荐
- snmp trap编写
1.MIB库查看net-snmp的安装目录./usr/share/snmp/mibs目录下: NET-SNMP-EXAMPLES-MIB.mib本件部分内容如下: netSnmpExampleHear ...
- JavaScript 杂乱的小总结
基本类型只有String.number.boolean.null.undefined,还有一个Object.存在装箱类型,不过后台自动转换. 通过new创建对象时,如果没有参数,可以省略“()”.-- ...
- (转)x264源码分析(1):main、parse、encode、x264_encoder_open函数代码分析
转自:http://nkwavelet.blog.163.com/blog/static/2277560382013103010312144/ x264版本: x264-snapshot-2014 ...
- C# HttpClientHelper请求
public class HttpClientHelper { /// <summary> /// get请求 /// </summary> /// <param nam ...
- QListView的子项的ViewMode
QListView.setViewMode(ViewMode mode) enum QListView::ViewMode Constant Value DescriptionQListV ...
- jQuery-处理css样式
1.css方法 获取匹配元素集合中的第一个元素的样式属性的值 或 设置每个匹配元素的一个或多个CSS属性 1)获取并设置单个css值 jQuery对象.css('css属性'); jQuery对象.c ...
- 小明A+B(杭电2096)
/*小明A+B Problem Description 小明今年3岁了, 如今他已经可以认识100以内的非负整数, 而且可以进行100以内的非负整数的加法计算. 对于大于等于100的整数, 小明仅保留 ...
- 如何快捷地使用ChemBio 3D检查结构信息
ChemBio 3D是一款三维分子结构演示软件,能够轻松快捷地进行化学结构的制作和立体旋转.ChemBio 3D Ultra 14作为ChemBio 3D的最新版本可以更加快捷地制作化学结构.本教程将 ...
- linux 数据盘和系统盘的查看
系统盘就像linux的c盘,使用df -l命令查看 如下所示: 可以看到根路径 / 都是位于系统盘.而/root,/home,/usr就如同c盘下的c:\windows,c:\usr这些目录 如果单独 ...
- 关于代理ip
反爬很重要的手段之一就是根据ip来了,包括新浪微博搜索页 微信搜索页 360全系网站360搜索 360百科 360 问答 360新闻,这些都是明确的提示了是根据ip反扒的,所以需要买ip.买得是快代理 ...