poj 3349 哈希 简单拉链(卡时间过去的。。。)

大家好,昨天由于感觉到hash表作为一种数据结构的强壮能力,决定做些相关题练习一下,所以做了poj3349这一道。简单来看就是问一堆雪花有没有同构的,而在询问的过程中,经本人测试,用log n 时间插入及查询会超时,所以应该运用哈希表才能过去。雪花由于其对称性,可以举出其同构的12种类型,经比较后确定最小值并插入。解决冲突的方法采用了最原始的拉链法,最后3.7s过去的,惊险。别的方法我还会尝试的。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;
#define MAXN 100010
#define HASHMOD 100010
typedef struct ota {
	int a[6];
	struct ota(int aa[]) {
		for (int i = 0; i < 6; i++)
			a[i] = aa[i];
	}
	bool operator == (struct ota bb)  {
		for (int i = 0; i < 6; i++)
			if (a[i] != bb.a[i])
				return false;
		return true;
	}
}Ota;
bool isBigger(int a[], int b[])
{
	for (int i = 0; i < 6; i++)
		if (a[i] != b[i])
			return a[i] > b[i];
	return false;
}
vector<Ota > hashTable[MAXN];
void initHash()
{
	for (int i = 0; i < MAXN; i++)
		hashTable[i].clear();
}
int getHash(int a[])
{
	int ret = 0;
	for (int i = 0; i < 6; i++) {
		ret += a[i];
		ret %= HASHMOD;
	}
	return ret;
}
bool searchHash(int a[])
{
	int h = getHash(a);
	Ota temp = Ota(a);
	if (hashTable[h].size() == 0) {
		hashTable[h].push_back(temp);
		return true;
	}
	for (int i = 0; i < hashTable[h].size(); i++) {
		if (hashTable[h][i] == temp)
			return false;
	}
	hashTable[h].push_back(temp);
	return true;
}
int main()
{
	int num[2][12], n;//for storing all possible permutations efficiently
	bool twin = false;
	initHash();
	scanf("%d", &n);
	while (n--) {
		for (int i = 0; i < 6; i++) {
			scanf("%d", &num[0][i]);
			num[0][i + 6] = num[0][i];
		}
		if (twin)
			continue;
		for (int i = 0; i < 6; i++)
			num[1][i + 6] = num[1][i] = num[0][5 - i];
		int *tempMin;
		tempMin = num[0];
		for (int i = 0; i < 6; i++)
			if (isBigger(tempMin, num[0] + i))
				tempMin = num[0] + i;
		for (int i = 0; i < 6; i++)
			if (isBigger(tempMin, num[1] + i))
				tempMin = num[1] + i;
		if (searchHash(tempMin) == false)
			twin = 1;
	}
	if (twin)
		printf("Twin snowflakes found.\n");
	else
		printf("No two snowflakes are alike.\n");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值