个人表示对哈希完全的无语,做题速度完全取决于你的运气。
取模取的巧的话,就可以很快的解出来,而像我这样,连续re了好几次的,也需要人品啊~~~
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#define max 100001
struct list
{
int a[6];
struct list *next;
}*hash[max+2],*p;
int bijiao(int *x,int *y)
{
int leap=1,i;
sort(x,x+6);
sort(y,y+6);
for(i=0;i<6;i++)
{
if(x[i]!=y[i])return 0;
}
return leap;
}
int main()
{
int aa[10];
int n,i,j,k;
int leap=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
int sum=0;
for(j=0;j<6;j++)
{
scanf("%d",&aa[j]);
sum+=aa[j];
}
int x=sum%max;
if(hash[x]==NULL)
{
hash[x]=new list;
hash[x]->next=NULL;
for(j=0;j<6;j++)
{
hash[x]->a[j]=aa[j];
}
}
else
{
p=hash[x];
while(p)
{
if(bijiao(p->a,aa))
{
leap=1;
}
p=p->next;
}
if(leap==0)
{
p=new list;
for(k=0;k<6;k++)
{
p->a[k]=aa[k];
}
p->next=NULL;
}
}
}
if(leap==1)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.\n");
return 0;
}