http://acm.pku.edu.cn/JudgeOnline/problem?id=3349
题意为判断是后有相同的雪花,雪花的花瓣可能是顺时针,或逆时针描述,故对hash值相同的还要进行比较,相同则推出,否则把该雪花加到该hash对应的链表上
code:(时间复杂度还是太高了)
- #include<iostream>
- using namespace std;
- struct node
- {
- int flag;
- int a[6];
- node *next;
- }hash[150006];
- int fac(int tt[]);
- int comp(int a[],int b[]);
- int main()
- {
- freopen("in.txt","r",stdin);
- int t[6];
- int n,i,sum,j;
- int flag(1);
- for( i=0; i<100001; i++)
- {
- hash[i].flag=0;
- hash[i].next=NULL;
- }
- cin>>n;
- for( i=0; i<n; i++)
- {
- for( j=0; j<6; j++)
- scanf("%d",&t[j]);
- sum=fac(t);
- if(!hash[sum].flag)
- {
- hash[sum].flag=1;
- memcpy(hash[sum].a,t,sizeof(t));
- }
- else
- {
- node temp=hash[sum];
- node temp1;
- temp1.flag=0;
- temp1.next=NULL;
- memcpy(temp1.a,t,sizeof(t));
- while(1)
- {
- if(comp(temp.a,t))
- {
- cout<<"Twin snowflakes found."<<endl;
- exit(-1);
- }
- if(temp.next==NULL)
- break;
- temp=*temp.next;
- }
- temp.next=&temp1;
- }
- }
- cout<<"No two snowflakes are alike."<<endl;
- return 0;
- }
- int fac(int tt[])
- {
- __int64 sum(0);
- int i;
- for( i=0; i<6; i++)
- sum+=tt[i];
- return sum%149997;
- }
- int comp(int a[],int b[])
- {
- int i,j,t;
- for( i=0; i<6; i++)
- if(a[i]==b[0])
- {
- if(i==6)
- return 0;
- for( j=1; j<6; j++)//往前
- {
- if((i+j)>=6)
- t=(i+j)-6;
- else
- t=i+j;
- if(a[t]==b[j])
- continue;
- else
- break;
- }
- if(j==6)
- return 1;
- for( j=1; j<6; j++)//往后
- {
- if(i-j<0)
- t=i-j+6;
- else
- t=i-j;
- if(a[t]==b[j])
- continue;
- else
- break;
- }
- if(j==6)
- return 1;
- }
- return 0;
- }