相同的雪花

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
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.
 
输入
The first line of the input will contain a single interger T(0<T<10),the number of the test cases.
The first line of every test case 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.
输出
For each test case,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.
样例输入
  1. 1
  2. 2
  3. 1 2 3 4 5 6
  4. 4 3 2 1 6 5
样例输出
  1. Twin snowflakes found.
  2.  
  3. 注意hash表大小
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<deque>
  9. #include<iomanip>
  10. #include<vector>
  11. #include<cmath>
  12. #include<map>
  13. #include<stack>
  14. #include<set>
  15. #include<fstream>
  16. #include<memory>
  17. #include<list>
  18. #include<string>
  19. using namespace std;
  20. typedef long long LL;
  21. typedef unsigned long long ULL;
  22. #define MAXN 100001
  23. #define INF 1000000009
  24. #define eps 0.00000001
  25. /*
  26. 寻找是否有两片相同的雪花
  27. 雪花的边可能从任意一边 开始!
  28. Hash 雪花总边长
  29. */
  30. typedef struct Hashnode
  31. {
  32. int a[];
  33. struct Hashnode* next;
  34. }*List;
  35. typedef struct HashT
  36. {
  37. List* L;
  38. }*HashTable;
  39. int NextPrime(int n)
  40. {
  41. int i;
  42. for (int tmp = n;; tmp++)
  43. {
  44. for (i = ; i*i <= tmp; i++)
  45. {
  46. if (tmp%i == )
  47. break;
  48. }
  49. if (i*i > tmp)
  50. return i;
  51. }
  52. }
  53. HashTable Init(int n)
  54. {
  55. HashTable H = (HashTable)malloc(sizeof(HashT));
  56. H->L = (List*)malloc(sizeof(List)*MAXN);
  57. for (int i = ; i < MAXN; i++)
  58. {
  59. H->L[i] = (List)malloc(sizeof(Hashnode));
  60. memset(H->L[i]->a, , sizeof(H->L[i]->a));
  61. H->L[i]->next = NULL;
  62. }
  63. return H;
  64. }
  65. void Free(HashTable H)
  66. {
  67. for (int i = ; i < MAXN; i++)
  68. free(H->L[i]);
  69. free(H->L);
  70. free(H);
  71. }
  72. int Hash(int sum, int h)
  73. {
  74. return sum%h;
  75. }
  76. List Find(int a[], int sum, HashTable H)
  77. {
  78. List p = H->L[Hash(sum, MAXN)];
  79. List l = p->next;
  80. int i;
  81. while (l != NULL)
  82. {
  83. for (int j = ; j < ; j++)
  84. {
  85. if (l->a[j] == a[])
  86. {
  87. for (i = ; i < ; i++)
  88. {
  89. if (l->a[(j + i) % ] != a[i])
  90. break;
  91. }
  92. if (i == ) return l;
  93. for (i = ; i < ; i++)
  94. {
  95. if (l->a[(j - i + ) % ] != a[i])
  96. break;
  97. }
  98. if (i == ) return l;
  99. }
  100. }
  101. l = l->next;
  102. }
  103. return NULL;
  104. }
  105. bool Insert(int a[], int sum, HashTable H)
  106. {
  107. List L = H->L[Hash(sum, MAXN)];
  108. List p = Find(a, sum, H);
  109. if (p == NULL)
  110. {
  111. List tmp = (List)malloc(sizeof(Hashnode));
  112. for (int i = ; i < ; i++)
  113. tmp->a[i] = a[i];
  114. tmp->next = L->next;
  115. L->next = tmp;
  116. return true;
  117. }
  118. return false;
  119. }
  120. int main()
  121. {
  122. int T, n, tmp[];
  123. bool f;
  124. scanf("%d", &T);
  125. while (T--)
  126. {
  127. scanf("%d", &n);
  128. f = false;
  129. HashTable H = Init(n);
  130. for (int i = ; i < n; i++)
  131. {
  132. int sum = ;
  133. for (int j = ; j < ; j++)
  134. {
  135. scanf("%d", &tmp[j]);
  136. sum += tmp[j];
  137. }
  138. if (!f && !Insert(tmp, sum, H))
  139. f = true;
  140. }
  141. if (f)
  142. printf("Twin snowflakes found.\n");
  143. else
  144. printf("No two snowflakes are alike.\n");
  145. Free(H);
  146. }
  147. return ;
  148. }

相同的雪花 Hash的更多相关文章

  1. nyoj-130-相同的雪花(hash)

    题目链接 /* Name:NYOJ-130-相同的雪花 Copyright: Author: Date: 2018/4/14 15:13:39 Description: 将雪花各个分支上的值加起来,h ...

  2. POJ 3349 HASH

    题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...

  3. 0x14 hash

    被虐爆了 cry 我的hash是真的菜啊... poj3349 肝了一个上午心态崩了...一上午fail了42次我的天,一开始搞了个排序复杂度多了个log,而且是那种可能不同值相等的hash,把12种 ...

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

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

  5. 【hash表】收集雪花

    [哈希和哈希表]收集雪花 题目描述 不同的雪花往往有不同的形状.在北方的同学想将雪花收集起来,作为礼物送给在南方的同学们.一共有n个时刻,给出每个时刻下落雪花的形状,用不同的整数表示不同的形状.在收集 ...

  6. Acwing:137. 雪花雪花雪花(Hash表)

    有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,…,ai,6ai,1,ai,2,…,ai,6. 因为雪花的形状是封闭的环形,所以 ...

  7. NYOJ130 同样的雪花 【Hash】

    同样的雪花 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 You may have heard that no two snowflakes are alike. ...

  8. POJ 3349:Snowflake Snow Snowflakes(数的Hash)

    http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K T ...

  9. [poj3349]Snowflake Snow Snowflakes(hash)

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

随机推荐

  1. Paratroopers(最小割模型)

    http://poj.org/problem?id=3308 题意:一个m*n的网格,有L位火星空降兵降落在网格中,地球卫士为了能同时消灭他们,在网格的行或列安装了一个枪支,每行或每列的枪支都能消灭这 ...

  2. easyui form.rest和clear 重置表单和清除表单数据区别

    easyui中的一般我们在新增和编辑的时候 都是用一个form表单 那新增的时候 需要重置下表单内容,一般用 $('#EditForm').form('reset'); 大部分时候没问题,但是如果表单 ...

  3. Bootstrap3模态框Modal垂直居中样式

    1,Bootstrap 模态框插件Bootbox垂直居中样式: <!DOCTYPE html> <html lang="en"> <head> ...

  4. Android O Bitmap 内存分配

      我们知道,一般认为在Android进程的内存模型中,heap分为两部分,一部分是native heap,一部分是Dalvik heap(实际上也是native heap的一部分).   Andro ...

  5. VUE路由history模式坑记--NGINX

    因微信分享和自动登录需要,对于URL中存在'#'的地址,处理起来比较坑(需要手动写一些代码来处理).还有可能会有一些隐藏的问题没被发现. 如果VUE能像其他(JSP/PHP)系统的路径一样,就不存在这 ...

  6. __declspec(dllexport)

    __declspec(dllexport) (2010-06-17 10:04:28) 转载▼ 标签: __declspec dllexport 导出 it 分类: C 先看代码:以下是在dev-c+ ...

  7. BeginEditorCommand()

    BeginEditorCommand();开始把焦点给CAD CompleteEditorCommand();焦点给窗体

  8. PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unkno…

    今天安装安装一个叫得推校园O2O系统的使劲都说连接不上服务器. 下面是安装说明,我直接进行3步骤,导入数据库配置文件,结果就显示题目报错的内容 安装说明: 直接输入程序目录即可 http://loca ...

  9. 20190625 Oracle优化查询(一)

    与其惴惴不安,不如定心应变 前提:我的Oracle服务器是安装在Windows环境中的,没有上到Linux 查看表结构 查询全表 查找空值, 使用“=”是没有结果的,应该使用IS NULL

  10. C语言中的DEBUG

    #cat aa.c #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include < ...