判断两个雪花是否一样
n=100000;
对每个雪花做一次hash,hash值相同的建立一个链表。
用的是数组模拟链表的方法。
hash函数也用的很简单的办法,全部相加然后求模
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=5111111;
int next[maxn];
int lon;
struct
{
int next;
int a[7];
}e[1111111];
int cmp(int tmp[],int txt[])
{
int ture;
for(int k=0;k<6;k++)
{
ture=1;
for(int i=1;i<=6;i++)
if(tmp[i]!=txt[(i+k-1)%6+1])
{
ture=0;
break;
}
if(ture)
return(1);
}
for(int k=0;k<6;k++)
{
ture=1;
for(int i=1,j=6;i<=6;i++,j--)
if(tmp[j]!=txt[(i+k-1)%6+1])
{
ture=0;
break;
}
if(ture)
return(1);
}
return(0);
}
int check(int tmp[])
{
int txt=0;
for(int i=1;i<=6;i++)
txt+=tmp[i];
txt%=maxn;
if(next[txt]==-1)
{
next[txt]=++lon;
for(int i=1;i<=6;i++)
e[lon].a[i]=tmp[i];
}
else
{
int u=next[txt];
while(e[u].next!=0)
{
if(cmp(tmp,e[u].a)==1)
return(1);
u=e[u].next;
}
if(cmp(tmp,e[u].a)==1)
return(1);
e[u].next=++lon;
for(int i=1;i<=6;i++)
e[lon].a[i]=tmp[i];
}
return(0);
}
int main()
{
int n;
scanf("%d",&n);
int tmp[10];
memset(next,-1,sizeof(next));
memset(e,0,sizeof(e));
lon=0;
int i,ture=0;
for(i=1;i<=n;i++)
{
for(int k=1;k<=6;k++)
scanf("%d",&tmp[k]);
if(check(tmp)==1)
{
ture=1;
break;
}
}
for(i++;i<=n;i++)
for(int k=1;k<=6;k++)
scanf("%d",&tmp[k]);
if(ture)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.\n");
return 0;
}