Title Link: http://poj.org/problem?id=3349
Test instructions: You may have heard that there are no two identical snowflakes in the world, we define a snowflake with 6 lobes, if there are 2 snowflakes the same [snowflakes are annular, so the same can be rotated after the same] then output "Twin snowflakes found.", otherwise output "No two Snowflakes is alike. "
Idea: The simplest is 22 judgment, but this complexity is O (n^2), TLE. So we have to try to reduce the number of judgments, we use the 6-petal snowflake Hash into a number, only two snowflakes with the same hash value only "may" the same, and then there will inevitably be a hash of the conflict, so the zipper method to resolve the conflict.
#include <iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<vector>#include<cmath>#include<time.h>#include<Set>using namespaceStd;typedefLong Long intLL;Const intmaxn=100000+5;Const intMod=999991;intnum[maxn][6];//Data Storagestructnode{intId,next;} SNOW[MAXN];intSCNT,HEAD[MOD];//List TablevoidInit () {//initializationmemset (head,-1,sizeof(Head)); Scnt=0;}voidAddNode (intHashn,intidx) {Snow[scnt].id=idx; Snow[scnt].next=Head[hashn]; HEAD[HASHN]=scnt++;}BOOLcmpintIdxintIdy) {//Compare Snowflake for(intI=0;i<6; i++) {//Sequence Order BOOLflag=true; for(intst=i,j=0;j<6; J++,st= (st+1==6?0: st+1)){ if(num[idx][st]!=Num[idy][j]) {Flag=false; } } if(flag) {return true; } } for(intI=0;i<6; i++) {//Reverse Order BOOLflag=true; for(intst=i,j=0;j<6; J++,st= (st-1==(-1)?5: st-1)){ if(num[idx][st]!=Num[idy][j]) {Flag=false; } } if(flag) {return true; } } return false;}BOOLSolveintID) { inthashnum=0; for(intI=0;i<6; i++) {//Make HashHashnum= (hashnum%mod+ (Num[id][i])%mod)%MOD; } for(inti=head[hashnum];i!=-1; i=snow[i].next) {//get the same hash value if(CMP (id,i)) {//Compare return true; }} AddNode (Hashnum,id); //INSERT into hash table return false;}intMain () {#ifdef Kirito freopen ("In.txt","R", stdin); Freopen ("OUT.txt","W", stdout);#endif intstart=clock (); intN,val; while(~SCANF ("%d",&N)) { BOOLflag=false; Init (); for(intI=0; i<n;i++){ for(intj=0;j<6; j + +) {scanf ("%d",&Num[i][j]); } if(flag) {Continue;} if(Solve (i)) {flag=true; } } if(!flag) {printf ("No. Snowflakes is alike.\n"); } Else{printf ("Twin Snowflakes found.\n"); }} #ifdef local_time cout<<"[Finished in"<< clock ()-Start <<"MS]"<<Endl;#endif return 0;}
POJ 3349 HASH