Snowflake Snow Snowflakes

Miku

hash一波

然后拉链法散列表

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>inline void read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c))f^=c=='-',c=getchar();
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar('0'+x%10);
}
int n;
int snow[100005][7];
vector <int> has[100008];
int mod=100007;
bool cmp(int x,int y){
	for(int i=0;i<=5;++i){
		if(snow[x][0]==snow[y][i]){
	//		cout<<i<<endl;
			int f=0;
			for(int j=1;j<=5;++j){
				if(snow[x][j]!=snow[y][(i+j)%6]){
					f=1;
					break;
				}
			}
			if(!f) return 1;
			f=0;
			for(int j=1;j<=5;++j){
				if(snow[x][j]!=snow[y][(i-j+6)%6]){
				//cout<<j<<endl;
					f=1;
					break;
				}
			}
			if(!f) return 1;
		}
	}
	return 0;
}
void add(int x){
	int sum=0;
	for(int i=0;i<=5;++i){
		sum+=snow[x][i];
	}
	sum%=mod;
	for(int ke=0;ke<has[sum].size();++ke){
		if(cmp(x,has[sum][ke])){
			cout<<"Twin snowflakes found.";
			exit(0);
		}
	}
	has[sum].push_back(x);
}
int main(){
	read(n);
	for(int i=1;i<=n;++i){
		for(int j=0;j<6;++j){
			read(snow[i][j]);
			
		}
		add(i);
	}
	cout<<"No two snowflakes are alike.";
	return 0;
}
上一篇:Keepalived & LVS 搭建高可用的Web服务


下一篇:三十分钟学会AWK