Snowflake Snow Snowflakes POJ - 3349(hash)
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. 题意:给n朵雪花,每个雪花都有6条边,找出是否有两个相同的雪花。
思路:n²遍历肯定不行,然后就想到hash,对于存放hash值的邻接表,我用的是vector,事实证明很慢啊,手写前向星快很多。
hash:我用的是直接相加然后取模一个素数的方法,当然还有很多其他方法,各种异或或者骚运算应该都差不多。 (对于equl函数,我错了很久,之前写的是用while找到一个pos位置和b【0】相同,就退出,然后我发现,找到一个不够,也许有下一个不能直接退出)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int SIZE = ;
int n,tot,p=;
int snow[SIZE][];
vector<int>v[SIZE]; int h(int a)
{
int sum=;
for(int i=;i<;i++)
{
sum = (sum + snow[a][i]) % p;
}
return sum % p;
}; bool equl(int a,int b)
{
for(int j=;j<;j++)
{
if(snow[a][j] != snow[b][])continue;
int flag = ;
for(int i=; i<; i++)
{
if(snow[a][(j + i)%] != snow[b][i] )
flag = ;
}
if(flag)
return ;
flag = ;
for(int i=; i<; i++)
{
if(snow[a][(j-i+)%] != snow[b][i])
flag = ;
}
if(flag) return ;
}
return ;
} bool Insert(int a)
{
int val = h(a);
int len = v[val].size();
for(int i=;i<len;i++)
{ if(equl(v[val][i],a))return ;
}
v[val].push_back(a);
return ;
} int main()
{
scanf("%d",&n);
int flag = ;
for(int i=;i<=n;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&snow[i][j]);
}
if(!flag && Insert(i))
{
puts("Twin snowflakes found.");
flag = ;
}
}
if(!flag)
puts("No two snowflakes are alike.");
}
Snowflake Snow Snowflakes POJ - 3349(hash)的更多相关文章
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...
- Snowflake Snow Snowflakes POJ - 3349 Hash
题意:一个雪花有六个角 给出N个雪花 判断有没有相同的(可以随意旋转) 参考:https://blog.csdn.net/alongela/article/details/8245005 注意:参考 ...
- Snowflake Snow Snowflakes - poj 3349 (hash函数)
判断n朵雪花中,是否有完全一样的雪花.简单的hash,将雪花的六个边的权值加起来,记为sum,将sum相等的雪花归为一类,再在这里面根据题意找完全相同的,判断顺时针或者逆时针的所有角是否一模一样. # ...
- POJ 3349:Snowflake Snow Snowflakes(数的Hash)
http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K T ...
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- POJ 3349 Snowflake Snow Snowflakes(简单哈希)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 39324 Accep ...
- [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30512 Accep ...
- poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30529 Accep ...
- POJ--3349 Snowflake Snow Snowflakes(数字hash)
链接:Snowflake Snow Snowflakes 判断所有的雪花里面有没有相同的 每次把雪花每个角的值进行相加和相乘 之后hash #include<iostream> #incl ...
随机推荐
- centos7搭建smb服务
1 yum install samba samba-client samba-common -y 安装smb服务 2 cp -a /etc/samba/smb.conf /etc/samba/sm ...
- JUnit-三角形判断测试
添加工具 1.添加JUnit测试工具: 使用eclipse自带的JUnit或者下载相关包.使用方式如下: 新建一个项目后,点击next出现以下界面: 选择添加JUnit 选择完成出现以下目录文件: p ...
- 如何修改PDF文件内容,PDF怎么添加背景
很多的情况下,大家都会遇到PDF文件,不管是在学习中还是在工作中,对于PDF文件,文件的修改编辑是需要用到PDF编辑软件的,在编辑文件的时候,发现文件的页面是有背景颜色的,又该如何修改背景颜色呢,不会 ...
- C++ LocalAlloc() & LocalSize() & LocalFree ()
关于LocalAlloc function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa366723(v=vs.85). ...
- axure交互样式(下拉列表和矩形)
*****矩形交互样式之单选按钮*****1.账号输入框.密码输入框等文本框实现效果:输入框获取焦点时边框是蓝色,失 去焦点时边框为红色: 2.实现思路:边框用矩形来设置选中和未选中.禁用和启用即可 ...
- 步步为营-77-Ajax简介
AJax:异步JavaScript和XML.Asynchronous JavaScript and XML 优点:无刷新 1 JavaScript下的Ajax 1.1 XMLHttpRequest对象 ...
- 20165206 2017-2018-2 《Java程序设计》第三周学习总结
20165206 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 类:class是关键字,用来定义类. 类声明:例如class People. 对象的声明:类 ...
- java 解析域名得到host
// 形如https://www.baidu.com 或 www.baidu.com, 判断这两种情况,并解析前者去掉http头,传入domain host // 方案1:正则表达式 + URI解析方 ...
- 通过expdp和impdp将Oracle11g数据导入到Oracle10g中
1 导出过程 1.1 查看目录: select * from dba_directories; 1.2 将目录的操作权限赋值给指定的用户(不执行次步骤可能会出现权限问题): grant read,wr ...
- web应用启动后发现被自动访问
为了找到原因,做了以下操作,发现是eclipse访问的,但是具体原因未知