POJ 3349.Snowflake Snow Snowflakes

题目:http://poj.org/problem?id=3349

AC代码(C++):

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <bitset>

#define INF 0xfffffff
#define MAXN 1200010
#define prime 1200007

using namespace std;

struct NODE{
	int num[6];
	int next;
};

NODE node[MAXN];
int hash[MAXN];
int nodecnt;

bool cmp(int* num1, int* num2){
	for(int i = 0; i < 6; i++){
		if(num1[i]!=num2[i])return false;
	}
	return true;
}

int getHash(int* num){
	return (num[0]%prime + num[1]%prime + num[2]%prime + num[3]%prime + num[4]%prime + num[5])%prime;
}

void insertHash(int* num, int key){
	for(int i = 0; i < 6; i++)node[nodecnt].num[i] = num[i];
	node[nodecnt].next = hash[key];
	hash[key] = nodecnt;
	nodecnt++;
}

bool searchHash(int* num){
	int key = getHash(num);
	int next = hash[key];
	while(next!=-1){
		if(!cmp(num,node[next].num)){
			next = node[next].next;
		}
		else return true;
	}
	insertHash(num,key);
	return false;
}

int main(){
	int n;
	cin>>n;
	bool flag = false;
	nodecnt = 0;
	for(int i = 0; i < MAXN; i++)hash[i] = -1;
	while(n--){
		int num[2][12];
		for(int i = 0; i < 6; i++){
			scanf("%d",&num[0][i]);
			num[0][i+6] = num[0][i];
			num[1][5-i] = num[0][i];
			num[1][5-i+6] = num[0][i];
		}
		if(flag)continue;
		
		for(int i = 0; i < 6; i++){
			if(searchHash(num[0]+i)||searchHash(num[1]+i)){
				flag = true;
				break;
			}
		}
	}
	if(flag)cout<<"Twin snowflakes found.";
	else cout<<"No two snowflakes are alike.";
}
总结: 数字哈希. 顺逆时针和顺序需要注意下, 还有就是这题时间很紧, 能优化的地方要优化下. 其实我这个代码还有待改善, 首先是时间太紧3719ms差点TLE, 其次如果输入1 1 1 1 1 2这样的雪花就会直接认为有相同, 因为在insert完顺时针i = 0的时候已经插入了1 1 1 1 1 2, 在search逆时针i = 1时直接搜到了刚才插入的1 1 1 1 1 2, 所以认为是相同的. 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值