poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30529 | Accepted: 8033 |
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
#include <iostream>
#include <stdio.h> using namespace std; #define MAXPRIME 100001 struct Hashnode{
int len[];
Hashnode* next;
};
Hashnode hashtable[MAXPRIME]={}; //哈希表,拉链法解决哈希冲突 int getkey(Hashnode* p) //哈希函数,求和取余法。求一片雪花对应的key值
{
int i,key=;
for(i=;i<;i++)
key = (key + p->len[i])%MAXPRIME;
return key;
} bool clockwise(Hashnode* p,Hashnode* q) //顺时针看有没有相同的顺序
{
int i,j;
for(i=;i<;i++){
for(j=;j<;j++)
if(p->len[j]!=q->len[(i+j)%])
break;
if(j>=)
break;
}
//找到两片相同的雪花
if(i<)
return true;
else
return false;
} bool counterclockwise(Hashnode* p,Hashnode* q) //逆时针看有没有相同的顺序
{
int i,j;
for(i=;i<;i++){
for(j=;j<;j++)
if(p->len[j]!=q->len[(i+-j)%])
break;
if(j>=)
break;
}
//找到两片相同的雪花
if(i<)
return true;
else
return false;
} bool iscom(Hashnode* p) //判断这片雪花是否和之前的有一片雪花完全相同
{
int key = getkey(p);
if(hashtable[key].next==NULL){ //没有冲突
hashtable[key].next = p;
return false;
}
else{ //产生冲突
//拉链法解决冲突
Hashnode* q = &hashtable[key];
p->next = q->next;
q->next = p; q = p->next; //从p的下一个开始比较
while(q){
if(clockwise(p,q) || counterclockwise(p,q)) //顺时针或者逆时针看有一个方向有相同的顺序,说明找到了相同的雪花
return true;
q = q->next;
}
return false;
}
} int main()
{
int n,i;
bool f = false; //找没找到两片相同的雪花,默认是没有
scanf("%d",&n);
while(n--){
Hashnode* p = new Hashnode;
p->next = NULL;
for(i=;i<;i++) //读取这片雪花的6个花瓣的长度
scanf("%d",&p->len[i]);
if(iscom(p)){ //判断这片雪花是否和之前的有一片雪花相同,如果有,改变f的值。
//函数中顺便将这片花瓣的信息插入到哈希表。
f=true;
break; //没有输入完毕,中途退出也可以,省了300MS
}
}
//判断有没有找到两片一样的雪花
if(f)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.\n");
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)的更多相关文章
- [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30512 Accep ...
- 哈希—— POJ 3349 Snowflake Snow Snowflakes
相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions ...
- POJ 3349 Snowflake Snow Snowflakes(简单哈希)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 39324 Accep ...
- POJ 3349 Snowflake Snow Snowflakes
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- POJ 3349 Snowflake Snow Snowflakes (哈希表)
题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...
- POJ 3349 Snowflake Snow Snowflakes(哈希表)
题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes fou ...
- POJ - 3349 Snowflake Snow Snowflakes (哈希)
题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花.若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相 ...
- POJ 3349 Snowflake Snow Snowflakes(哈希)
http://poj.org/problem?id=3349 题意 :分别给你n片雪花的六个角的长度,让你比较一下这n个雪花有没有相同的. 思路:一开始以为把每一个雪花的六个角的长度sort一下,然后 ...
随机推荐
- fzu2188 状压dp
G - Simple String Problem Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- gollum安装教程
在线markdown编辑器,可以直接将该程序安装在服务器上,直接编辑完之后保存在gollum目录下 1.在线安装 sudo apt-get install ruby1.9.1 ruby1.9. ...
- java微信接口之——获取access_token
本文转自http://www.cnblogs.com/always-online/category/598553.html 一.微信获取access_token接口简介 1.请求:该请求是GET方式请 ...
- ionic 接触的第一个Hybrid项目
最近需要维护一个Hybird项目,使用的是ionic,由于是第一个Hybrid项目,在这里记录下基本的知识. 先看一下ionic的最基本介绍: http://my.oschina.net/u/2275 ...
- 数据结构——B树、B+树
B树和B+树主要应用于外排序,对于外排序,从硬盘读取的时间要远远大于遍历树的时间,因此要想办法减少从硬盘读取的时间. B树(有时也叫B-树) M阶B树定义如下: 是一种多路搜索树(并不是二叉的):1. ...
- 初识Python(一)
Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...
- centos6.5kvm虚拟化安装部署
一.走进云计算 云计算:云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供, ...
- Match:DNA repair(POJ 3691)
基因修复 题目大意:给定一些坏串,再给你一个字符串,要你修复这个字符串(AGTC随便换),使之不含任何坏串,求修复所需要的最小步数. 这一题也是和之前的那个1625的思想是一样的,通过特殊的trie树 ...
- KMP单模快速字符串匹配算法
KMP算法是由Knuth,Morris,Pratt共同提出的算法,专门用来解决模式串的匹配,无论目标序列和模式串是什么样子的,都可以在线性时间内完成,而且也不会发生退化,是一个非常优秀的算法,时间复杂 ...
- 【leetcode】 Unique Binary Search Trees II (middle)☆
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...