1. 程式人生 > >poj3349(數字hash)

poj3349(數字hash)

memcpy sin scanf clas const code ret long bool

#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=100000+10;
const int p=99991;

int a[7];
bool check[maxn];
int next[maxn],adj[maxn];
int snow[maxn][10];
int tot;

int H(int x[]){
    int h1=0,h2=1;
    for (int i=0;i<6;i++){
        h1=(h1%p+x[i]%p)%p;
        h2=(long
long)h2*x[i]%p; } return (h1%p+h2%p)%p; } bool cmp(int x[],int y[]){ bool en=1; for (int i=0;i<6;i++){ for (int j=0;j<6;j++){ en=1; for (int k=0;k<6;k++) if(x[(i+k)%6]!=y[(j+k)%6]) en=0; if(en) return 1; en=1;
for (int k=0;k<6;k++) if(x[(i+k)%6]!=y[(j-k+6)%6]) en=0; if(en) return 1; } } return 0; } bool myinsert(int x[]){ int val=H(x); for (int i=adj[val];i;i=next[i]){ if(cmp(snow[i],x)) return 1; } ++tot; memcpy(snow[tot],x,6*sizeof(int)); next[tot]
=adj[val]; adj[val]=tot; return 0; } int main(){ int n; bool is=0; scanf("%d",&n); while(n--){ for (int i=0;i<6;i++)scanf("%d",&a[i]); if(is) continue; if(myinsert(a)) { printf("Twin snowflakes found.\n"); is=true; } } if(!is)printf("No two snowflakes are alike.\n"); return 0; }

poj3349(數字hash)