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


 
  哈希表查找
  做的第一道哈希表的题,如有觉得不妥之处,欢迎指正和交流。
 
  题意
  在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长  度限制为1000000。
  两片雪花相等的条件:
  雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的)
 
  思路:
  大概思路就是用一个求和取余法来获得雪花的key值,用拉链法解决冲突。同时如果检测到冲突,依次判断一下这片雪花与和它key值相同的雪花是花瓣长度顺序是否相同,如果相同,证明找到了两片完全一样的雪花。退出循环,输出结果。
  详情可以见大神的博客: POJ3349-Snowflake Snow Snowflakes
  说的很详细。  
 
  注意
  所有的输入数据没有输入完毕就提前结束程序,无妨。例如没有输入完,就检测到相同的雪花,这个时候可以直接break跳出循环停止输入,不会影响评测结果。可以节省不少时间。
 
  代码
 #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(哈希查找,求和取余法+拉链法)的更多相关文章

  1. [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30512   Accep ...

  2. 哈希—— POJ 3349 Snowflake Snow Snowflakes

    相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions ...

  3. POJ 3349 Snowflake Snow Snowflakes(简单哈希)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 39324   Accep ...

  4. POJ 3349 Snowflake Snow Snowflakes

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...

  5. POJ 3349 Snowflake Snow Snowflakes (Hash)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 48646   Accep ...

  6. POJ 3349 Snowflake Snow Snowflakes (哈希表)

    题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...

  7. POJ 3349 Snowflake Snow Snowflakes(哈希表)

    题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes fou ...

  8. POJ - 3349 Snowflake Snow Snowflakes (哈希)

    题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花.若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相 ...

  9. POJ 3349 Snowflake Snow Snowflakes(哈希)

    http://poj.org/problem?id=3349 题意 :分别给你n片雪花的六个角的长度,让你比较一下这n个雪花有没有相同的. 思路:一开始以为把每一个雪花的六个角的长度sort一下,然后 ...

随机推荐

  1. 转: Annovar 软件注释流程介绍

    第一步:下载Annovar 上Annovar官网下载(http://annovar.openbioinformatics.org/en/latest/user-guide/download/),现在要 ...

  2. COGS 2416.[HZOI 2016]公路修建 & COGS 2419.[HZOI 2016]公路修建2 题解

    大意: [HZOI 2016]公路修建 给定一个有n个点和m-1组边的无向连通图,其中每组边都包含一条一级边和一条二级边(连接的顶点相同),同一组边中的一级边权值一定大于等于二级边,另外给出一个数k( ...

  3. (C语言)精髓——指针

    (1)作用:正确而灵活的运用指针,能够有效的表示复杂的数据结构,能动态分配内存,方便地使用字符串,有效而方便地使用数组,可以直接处理内存单元地址. (2)概念:①变量的指针:变量(3)的地址.(200 ...

  4. 普元部署多个应用的方法(适用EOS6.5以上版本,且无需governor中添加应用)

    在EOS下跑default项目之外的另外一个项目,比如defaultNew 步骤1 安装EOS6.5,安装路径如下:E:\program\eos: 启动EOS Eos默认的应用名称为Default 步 ...

  5. Linq查询非泛型集合要指定Student类型(比如List)

    #region Linq to 集合查询非泛型集合要指定Student类型            //ArrayList list = new ArrayList();            //li ...

  6. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  7. 利用ssh-copy-id无需密码登录远程服务器

    本地机器生成公钥和私钥 ssh-keygen -t rsa 一路回车,最后会在~/.ssh目录下生成id_rsa和id_rsa.pub这两个文件. 与远程服务器建立信任机制 ssh-copy-id - ...

  8. 树形dp汇总

    HDU 1520 HDU 2196 Codeforces 219D POJ 1155

  9. iOS 关于UIWindow的理解

    Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...

  10. net use与shutdown配合使用,本机重启远程服务器

    net use与shutdown配合使用,本机重启远程服务器   今天服务器出现问题了,能ping通,但就是远程登录服务器后,服务器无法响应.   在本机测试发现ftp服务可以使用,于是就想通过ftp ...