Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 25076 | Accepted: 6554 |
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 byn 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
解题思路:主要是hash,我用的是六角之和除以90001的模作为key值,在没有冲突的情况下,把雪花保存至向量中,有冲突的时候,调出之前key值相同的雪花进行对比,有同构的话对后续输入就不再处理,否则对这一key值的雪花加上这一片,然后再继续处理下一片雪花,所有雪花均处理完仍没有发现同构就输出没有发现同构。就是有一点我没想明白,我取模的时候用10w左右就行,100w就tle感觉不科学啊。。。不知道为什么。。。
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
long num[100010][10];
vector<int> Hash[90001];
bool same(int a,int b)//暴力打表,以6个角作为起点,顺时针和逆时针两种,6*2=12,一共12种情况
{
if(num[a][0]==num[b][0]&&num[a][1]==num[b][1]&&num[a][2]==num[b][2]&&num[a][3]==num[b][3]&&num[a][4]==num[b][4]&&num[a][5]==num[b][5])
return true;
else if(num[a][0]==num[b][0]&&num[a][1]==num[b][5]&&num[a][2]==num[b][4]&&num[a][3]==num[b][3]&&num[a][4]==num[b][2]&&num[a][5]==num[b][1])
return true;
else if(num[a][0]==num[b][5]&&num[a][1]==num[b][0]&&num[a][2]==num[b][1]&&num[a][3]==num[b][2]&&num[a][4]==num[b][3]&&num[a][5]==num[b][4])
return true;
else if(num[a][0]==num[b][1]&&num[a][1]==num[b][0]&&num[a][2]==num[b][5]&&num[a][3]==num[b][4]&&num[a][4]==num[b][3]&&num[a][5]==num[b][2])
return true;
else if(num[a][0]==num[b][4]&&num[a][1]==num[b][5]&&num[a][2]==num[b][0]&&num[a][3]==num[b][1]&&num[a][4]==num[b][2]&&num[a][5]==num[b][3])
return true;
else if(num[a][0]==num[b][2]&&num[a][1]==num[b][1]&&num[a][2]==num[b][0]&&num[a][3]==num[b][5]&&num[a][4]==num[b][4]&&num[a][5]==num[b][3])
return true;
else if(num[a][0]==num[b][3]&&num[a][1]==num[b][4]&&num[a][2]==num[b][5]&&num[a][3]==num[b][0]&&num[a][4]==num[b][1]&&num[a][5]==num[b][2])
return true;
else if(num[a][0]==num[b][3]&&num[a][1]==num[b][2]&&num[a][2]==num[b][1]&&num[a][3]==num[b][0]&&num[a][4]==num[b][5]&&num[a][5]==num[b][4])
return true;
else if(num[a][0]==num[b][2]&&num[a][1]==num[b][3]&&num[a][2]==num[b][4]&&num[a][3]==num[b][5]&&num[a][4]==num[b][0]&&num[a][5]==num[b][1])
return true;
else if(num[a][0]==num[b][4]&&num[a][1]==num[b][3]&&num[a][2]==num[b][2]&&num[a][3]==num[b][1]&&num[a][4]==num[b][0]&&num[a][5]==num[b][5])
return true;
else if(num[a][0]==num[b][1]&&num[a][1]==num[b][2]&&num[a][2]==num[b][3]&&num[a][3]==num[b][4]&&num[a][4]==num[b][5]&&num[a][5]==num[b][0])
return true;
else if(num[a][0]==num[b][5]&&num[a][1]==num[b][4]&&num[a][2]==num[b][3]&&num[a][3]==num[b][2]&&num[a][4]==num[b][1]&&num[a][5]==num[b][0])
return true;
return false;
}
int main()
{
long n;
scanf("%ld",&n);
long sum,key,i,j;
for(i=0;i<n;i++)
for(j=0;j<6;j++)
scanf("%ld",&num[i][j]);
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<6;j++)
sum+=num[i][j];
key=sum%90001;
for(vector<int>::size_type j = 0; j < Hash[key].size(); j++)
//对于每片雪花,之前没有出现过这一key值的话,Hash[key].size()为0,跳过循环
//把该雪花直接加入到向量中,否则对同一key值的雪花一一对比,未出现同构就把这一
//雪花加入向量的这一key值中,出现同构就直接输出找到相同的
{
if(same(Hash[key][j],i))
{
printf("%s\n", "Twin snowflakes found.");
return 0;
}
}
Hash[key].push_back(i);
}
printf("%s\n", "No two snowflakes are alike.");
return 0;
}