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 ...
随机推荐
- JavaScript利用键盘方向键(上下键)控制表格行选中
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 获取checkbox勾选的id
需求描述:做批量删除或者批量修改的时候需要获得多个id,根据checkbox勾选来获取对应的d 两种方法: //html代码<table id="table1"> &l ...
- 【linux】环境变量配置
假设要添加环境变量 JAVA_HOME 1.先用自己的个人账号 vim /etc/profile 在文件末尾添加 export JAVA_HOME=/usr/java/jdk1..0_144 2. s ...
- 古代猪文:数论大集合:欧拉定理,exgcd,china,逆元,Lucas定理应用
/* 古代猪文:Lucas定理+中国剩余定理 999911658=2*3*4679*35617 Lucas定理:(m,n)=(sp,tp)(r,q) %p 中国剩余定理:x=sum{si*Mi*ti} ...
- json数组
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>js ...
- 步步为营102-Css样式加个版本
背景:当系统发布后修改了css样式,由于浏览器有缓存,所以会造成css样式无效.可通过在css中添加版本号来解决 1 修改css引用 <link rel="stylesheet&quo ...
- python---数学表达式的分析树实现
先走一遍, 前面很多知道点,都串起来了. # coding = utf-8 # 使用列表实现栈的功能 class Stack: def __init__(self): self.items = [] ...
- [转] react-router4 + webpack Code Splitting
项目升级为react-router4后,就尝试着根据官方文档进行代码分割.https://reacttraining.com/react-router/web/guides/code-splittin ...
- Java集合中List,Set以及Map等集合体系详解(史上最全)
https://blog.csdn.net/zhangqunshuai/article/details/80660974
- CentOS7中Docker-ce的卸载和安装
一.查看是否已安装了Docker软件包: #查看是否已经安装的Docker软件包sudo yum list installed | grep docker 二.如果已安装不想要的docker/dock ...