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.
//开散列法解决冲突~ //雪花上限10000片 //雪花瓣的长度int型可存 //用结构体来存雪花 //此题用哈希法来做,用一个key来存雪花6片花瓣的总长度%prime得到一个数来代表这片雪花。 //在求key的时候,可以用同余模定理(顺便熟悉下嘛) /*key=( len1+len2+len3+len4+len5+len6)%prime =( len1%prime +len2%prime +len3%prime +len4%prime +len5%prime +len6)%prime */ //为了尽量减小地址冲突,应让prime值大些。取10W内最大的素数可以很好地减少冲突 //显然key值不同的雪花一定不可能相同,但是key相同的雪花是有可能不同的,只需将key值相同的雪花进行比较 #include <iostream> #include <cstdio> #include <vector> using namespace std; #define maxn 100008 #define prime 99983//10W内的最大素数 struct XH { int len[6]; int key; }xh[maxn]; bool vis[maxn];//用来判断是否有key值相等的雪花 vector <XH> ans[maxn]; bool cmp(XH a,XH b) { bool flag=false; for(int i=0;i<6;i++) { if(a.len[0]==b.len[i]) { int m=i; bool ok=true; for(int j=0;j<6;j++) { if(a.len[j]==b.len[m]) { m++; if(m>=6) m%=6; } else { ok=false; break; } } if(ok) { flag=true; return 1; } ok=true; m=i; for(int j=0;j<6;j++) { if(a.len[j]==b.len[m]) { m--; if(m<0) m=5;; } else { ok=false; break; } } if(ok) { flag=true; return 1; } } } for(int i=0;i<6;i++) { if(a.len[5]==b.len[i]) { int m=i; bool ok=true; for(int j=5;j>=0;j--) { if(a.len[j]==b.len[m]) { m--; if(m<0)m=5; } else { ok=false; break; } } if(ok) { flag=true; return 1; } m=i; ok=true; for(int j=5;j>=0;j--) { if(a.len[j]==b.len[m]) { m++; if(m>=6)m%=6; } else { ok=false; break; } } if(ok) { flag=true; return 1; } } } if(flag)return 1; return 0; } int main() { int n; while(scanf("%d",&n)!=EOF&&n) { bool flag=false; memset(vis,0,sizeof(vis)); for(int i=0;i<=n;i++) { ans[i].clear(); } for(int i=1;i<=n;i++) { for(int j=0;j<6;j++) { scanf("%d",&xh[i].len[j]); xh[i].key+=xh[i].len[j]%prime; } xh[i].key%=prime; } for(int i=1;i<=n;i++) { if(!vis[xh[i].key]) { vis[xh[i].key]=1; ans[xh[i].key].push_back(xh[i]); } else { for(int j=0;j<ans[xh[i].key].size();j++) { if(cmp(xh[i],ans[xh[i].key][j])) { flag=true; goto L; } } ans[xh[i].key].push_back(xh[i]); } } L: if(flag) { cout<<"Twin snowflakes found."<<endl; } else cout<<"No two snowflakes are alike."<<endl; } return 0; }