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
题解:
哈希,个人理解哈希就是一种暴力手法,选出符合要求的情况
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=1e6;
const int pri=999983;
struct node{
int a[6];
}x[MAXN];
vector<int >G[MAXM];
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
int main()
{
int n;
scanf("%d",&n);
int flag=0,MAX=0;
for (int i = 0; i <n ; ++i) {
for (int j = 0; j <6 ; ++j) {
scan_d(x[i].a[j]);
}
int z=0;
z=((x[i].a[0]+x[i].a[2]+x[i].a[4])&(x[i].a[1]+x[i].a[3]+x[i].a[5]))%pri;//此处还可以是,(a[0]+...+a[5])%pir
G[z].push_back(i);
}
int cnt=0;
for (int i = 0; i <=pri ; ++i) {
if(G[i].size()>=2)
{
for (int k = 0; k <G[i].size()-1 ; ++k) {
for (int l = k+1; l <G[i].size() ; ++l) {
for (int s=0;s<6;s++)//顺时针
{
cnt=0;
for (int j = 0,z=0; z<6,j <6 ; ++j,z++)
{
if(x[G[i][k]].a[j]!=x[G[i][l]].a[(z+s)%6])
{
cnt=1;
break;
}
}
if(cnt==0)
{
flag=1;
goto out;
}
}
for (int s=0;s<6;s++)//逆时针
{
cnt=0;
for (int j = 0,z=5; z>=0,j <6 ; ++j,z--)
{
if(x[G[i][k]].a[j]!=x[G[i][l]].a[(z-s+6)%6])
{
cnt=1;
break;
}
}
if(cnt==0)
{
flag=1;
goto out;
}
}
}
}
}
}
out:
if(flag)
{
printf("Twin snowflakes found.\n");
} else
printf("No two snowflakes are alike.\n");
return 0;
}