Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 30512   Accepted: 8024

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

  1. 2
  2. 1 2 3 4 5 6
  3. 4 3 2 1 6 5

Sample Output

  1. Twin snowflakes found.

Source

解题思路:

雪花有六个角。每一个角有一个长度。按顺时针或者逆时针给出,起点是随机的。两个雪花同样当且仅当每一个角的长度一样,且必须是连续的,比方   2 3 1  4 5 6 和 2 1 3  4 5 6就不行,1 2 3 4 5 6    4 3 2 1 6 5是一样的(第二个逆时针给出)。给出一堆雪花的六个角的长度。问这里面能不能找到两个同样的雪花。

1. 两个雪花同样的前提条件是,它们雪花六个角的长度和相等。

把每一个雪花依照长度映射到哈希表中。

这里哈希表用 vector< snowflake> hs[]. 当 hs[i].size()>1时,说明hs[i][j] 和hs[i][k]有可能是两个同样的雪花,仅仅要在hs[i]里面查找就能够了。

2. 怎么推断两个雪花a,b同样?分两步,一, 固定a,用b的每一个边和a的第一条边对齐,然后顺时针比較a,b的每一条边。二,固定a,用b的每一个边和a的第一条边对齐,然后逆时针比較a,b的每一条边。

Ps:以此题哀悼下考砸的数据结构。。

。。考试的时候我居然连拉链法解决冲突都不知道。。

代码:

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn=100002;
  8. const int prime=99991;
  9.  
  10. struct SnowFlake
  11. {
  12. int len[6];
  13. }snow[maxn];
  14.  
  15. vector<SnowFlake>hs[prime];
  16.  
  17. void Hash(SnowFlake s)//映射
  18. {
  19. int addr=(s.len[0]+s.len[1]+s.len[2]+s.len[3]+s.len[4]+s.len[5])%prime;
  20. hs[addr].push_back(s);
  21. }
  22.  
  23. bool cmp(SnowFlake a,SnowFlake b)//比較两个雪花是否同样
  24. {
  25. for(int i=0;i<6;i++)
  26. {
  27. if(a.len[0]==b.len[i]&&a.len[1]==b.len[(i+1)%6]&&a.len[2]==b.len[(i+2)%6]&&
  28. a.len[3]==b.len[(i+3)%6]&&a.len[4]==b.len[(i+4)%6]&&a.len[5]==b.len[(i+5)%6])
  29. return true;//固定a。顺时针比較
  30. if(a.len[0]==b.len[i]&&a.len[1]==b.len[(i+5)%6]&&a.len[2]==b.len[(i+4)%6]&&
  31. a.len[3]==b.len[(i+3)%6]&&a.len[4]==b.len[(i+2)%6]&&a.len[5]==b.len[(i+1)%6])
  32. return true;//固定a, 逆时针比較
  33. }
  34. return false;
  35. }
  36.  
  37. int main()
  38. {
  39. int n;
  40. scanf("%d",&n);
  41. SnowFlake sf;
  42. while(n--)
  43. {
  44. for(int i=0;i<6;i++)
  45. scanf("%d",&sf.len[i]);
  46. Hash(sf);
  47. }
  48. for(int i=0;i<prime;i++)
  49. if(hs[i].size()>1)//有可能有同样的雪花
  50. {
  51. for(int j=0;j<hs[i].size();j++)
  52. for(int k=j+1;k<hs[i].size();k++)
  53. {
  54. if(cmp(hs[i][j],hs[i][k]))
  55. {
  56. printf("Twin snowflakes found.");
  57. return 0;
  58. }
  59. }
  60. }
  61. printf("No two snowflakes are alike.");
  62. return 0;
  63. }

[ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)的更多相关文章

  1. 哈希—— POJ 3349 Snowflake Snow Snowflakes

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

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

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

  3. poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   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. hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes

    今天学的hash.说实话还没怎么搞懂,明天有时间把知识点总结写了,今天就小小的写个结题报告吧! 题意: 在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角 ...

  7. POJ 3349 Snowflake Snow Snowflakes(哈希)

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

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

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

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

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

随机推荐

  1. 使用国内镜像源安装npm包报错

    1 使用命令安装 npm install webpack --save-dev 可能是版本太高,国内没有更新. 通过设置镜像源 npm config set registry http://regis ...

  2. java与数据库交互常用到的一些方法

    下面我整理了一下java中常用的几个与数据库交互的常用方法,仅供参考: 1.执行SQL(dao层的实现类中) (1)SQL查询: //import org.hibernate.Query;//impo ...

  3. 《Effective Java》读书笔记四(泛型)

    Java1.5发行版本中增加了泛型(Generic).在没有泛型之前,从集合中读取到的每一个对象都必须进行转换.如果有人不小心插入了错误的类型对象,在运行时的转换处理就会出错.有了泛型之后,可以告诉编 ...

  4. Qt解析xml

    发现用 Qt 解析 xml 文件非常方便,下面是一个简单的解析 xml 文件的例子: #include <QtCore/QCoreApplication> #include <QDo ...

  5. js获取当前页面url网址等信息

    使用js获取当前页面的url网址信息. 1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.p ...

  6. 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distance@@QAE@H@Z),该符号在函数 _main 中被引用

    错误: 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Distance::Distance(int)" (??0Distanc ...

  7. Extjs加载本地的一个json文件

    <head>    <title></title>    <link href="../Styles/Extjs/resources/css/ext ...

  8. 【技术】正則表達式—匹配电话号码,网址链接,Email地址

    #pragma mark - 正则匹配电话号码.网址链接.Email地址 + (NSMutableArray *)addHttpArr:(NSString *)text { //匹配网址链接 NSSt ...

  9. exit和wait一起可以彻底清除子进程的资源

    #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<stdlib.h> ...

  10. CentOS ext4 磁盘分区 格式化 挂载

    [root@appserver ~]# df -h /*查看现有分区情况*/文件系统 容量 已用 可用 已用%% 挂载点/dev/vda1 9.9G 8.2G 1.2G 88% /tmpfs 3.9G ...