一、题目描述:
Snowflake Snow Snowflakes
Time Limit: 4000MS |
| Memory Limit: 65536K |
Total Submissions: 16476 |
| Accepted: 4203 |
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 片雪花,0 < n ≤ 100000 ,每片雪花是个六边形,每条边长度范围[0, 10^7) 。比如雪花:1 2 3 4 5 6 或者4 3 2 1 6 5 。按照顺时针或者逆时针顺序给出,所以上面两片雪花的构造是一致的(同构)。现在让你判断这n 片雪花里有没有同构的雪花,有的话,输出
Twin snowflakes found.
没有的话,输出:
No two snowflakes are alike.
三、解决方案
按照同构的概念,每种雪花至多有12 种表现形式,即在每条边互不相等的情况下,排在第一位的边有6 种选法,一旦排定,可以按照顺时针
或者逆时针两个方向读取,所以最多共12 种表现形式。而整个数据域的可能情况最少为10^42/12 ,数据量巨大,而实际的关键字集合的
数据量最多为n ,0 < n ≤ 100000 ,所以可以采用Hash 方法。关键字就选为雪花的6 条边组成的数组,判等就是判断是否同构。加上一个flag 表示Hash 表相应的槽位中是否有数据。定义如下:
#define ARM_NUM 6
typedef struct __Node{
int key[ARM_NUM]; // 雪花的表现形式
int flag; // 标志位,判断槽中是否有数据
}Node;
采用两个Hash 函数
h1(h) = sum(k) mod 100003
h2(h) =sum(k) mod 99991
sum(k) 表示雪花6 条边的数据求和。
采用双重散列的开放寻址法来解决碰撞问题。
h(k,i) = (h1(k) + h2(k)*i)mod 100003
从实际情况看,可以取得不错的效果:
Memory Time Language
2884K 2172MS C
如果采用线性探测,比如
h(k,i) = (h1(k) +i)mod 100003
测试结果TLE 。原因是碰撞太多,需要不停的探测新的槽位,导致超时。
四、C 代码