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.
所谓双胞胎雪花,就是六个花瓣长度都一样,For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.
因为花瓣的长度可以达到10000000,六个瓣就会很大,利用哈希思想,对其长度和模一个不大不小的素数。
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define maxin 14997 using namespace std; struct node { int xue[7];//存储一个花瓣的信息 }; struct node jie;//单节点变量 struct node mapp[15000][100];//二维数组,第一维记录花瓣的长度,第二维记录该长度下的每一个花朵 int m[15000];//记录下标长度下的花朵的数量 int bjj(node a,node b)//判断两个花瓣是否为双胞胎 { sort(a.xue,a.xue+6); sort(b.xue,b.xue+6); int i; for(i = 0; i < 6; i++) { if(a.xue[i] != b.xue[i]) return 0; } return 1; } int main() { int n,bj; int i,j,k,l; while(~scanf("%d",&n)) { memset(m,0,sizeof(m)); bj = 0; while(n--) { int sum = 0; for(i = 0; i < 6; i++) { scanf("%d",&jie.xue[i]); sum = (sum+jie.xue[i])%maxin;//花瓣的长度和对一个数取余 } if(bj == 0) { for(i = 0; i < m[sum]; i++) { int f = bjj(mapp[sum][i],jie); if(f == 1) { bj = 1; break; } } mapp[sum][m[sum]] = jie;//将该花朵加入记录数组 m[sum]++;//是该长度的花朵数量加一 } } if(bj == 1) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); } }