nyoj130 相同的雪花(hash)

 

相同的雪花

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
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.
 
输入
The first line of the input will contain a single interger T(0<T<10),the number of the test cases.
The first line of every test case 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.
输出
For each test case,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.
样例输入
1
2
1 2 3 4 5 6
4 3 2 1 6 5
样例输出
Twin snowflakes found.
 
算法分析:这个题是就是一个简单的 hash ;把每个数加起来 模上一个 比较大的 素数;就对有相同的hash值进行比较就行了
代码:
View Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define N 19373

using namespace std;

int hash[N][100][6];
int len[N];

bool judge(int a[6],int b[6])
{
int t[17],i,j,k;
for(i=0;i<=5;i++) t[i]=a[i];
for(i=6;i<=11;i++) t[i]=a[i-6];
for(i=12;i<=16;i++)t[i]=a[i-12];
for(i=5;i<=10;i++)
{
j=i;k=0;
while(t[j]==b[k]&&k<6)
{
j++;k++;
}
if(k==6) return true;
j=i;k=0;
while(t[j]==b[k]&&k<6)
{
j--;k++;
}
if(k==6) return true;
}
return false;
}

int main()
{
int test;
scanf("%d",&test);
while(test--)
{
memset(len,0,sizeof(len));
int n,i,j,t[6],s;
scanf("%d",&n);
for(j=0;j<n;j++)
{
s=0;
for(i=0;i<6;i++)
{
scanf("%d",t+i);
s+=t[i];
}
s=s%19373;
for(i=0;i<6;i++)
{
hash[s][len[s]][i]=t[i];
}
len[s]++;
}
int k;bool yes=false;
for(i=0;i<N&&!yes;i++)
{
if(len[i]==0||len[i]==1) continue;
for(j=0;j<len[i]&&!yes;j++)
{
for(k=j+1;k<len[i]&&!yes;k++)
if(judge(hash[i][j],hash[i][k]))
yes=true;
}
}
if(yes) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
}
return 0;
}

 
posted @ 2011-11-02 21:37  mtry  阅读(394)  评论(2编辑  收藏  举报