poj3349(数字hash)

#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;
}

猜你喜欢

转载自www.cnblogs.com/lmjer/p/8975921.html