Snowflake Snow Snowflakes(POJ 3349 Hash表)

SnowflakeSnowSnowflakes
Time Limit: 4000MS
Memory Limit: 65536K
Total Submissions: 43542
Accepted: 11420

Description

You may have heard that no two snowflakes(雪花) are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integern, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed byn lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise(顺时针方向的) orcounterclockwise(逆时针方向的), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.

Source

CCC 2007

题意:给出雪花的数量及雪花的每个花瓣的长度,问是否存在两片六个花瓣长度都相等的雪花。

  不能暴力比较,会TLE的。哈希表才是正解。看题解时有看到将花瓣长度排序后进行比较的解法,由于数据太水也AC了,但我认为这种解法是不符合题意的,如下代码里所用的比较方法才是正确的。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int max_size = 100010;
const int prime = 99991;
vector<int> hsh[prime];//存放关键码及相同关键值但不同组的数据的下标
int arm[max_size][6];//存放数据
int n;
bool isTheSame(int a, int b) {//比较两片雪花是否相同
	for (int i = 0; i<6; i++) {
		if ((//顺时针
			arm[a][0] == arm[b][i] && arm[a][1] == arm[b][(i + 1) % 6] &&
			arm[a][2] == arm[b][(i + 2) % 6] && arm[a][3] == arm[b][(i + 3) % 6] &&
			arm[a][4] == arm[b][(i + 4) % 6] && arm[a][5] == arm[b][(i + 5) % 6])
			||//逆时针
			(arm[a][0] == arm[b][i] && arm[a][1] == arm[b][(i + 5) % 6] &&
				arm[a][2] == arm[b][(i + 4) % 6] && arm[a][3] == arm[b][(i + 3) % 6] &&
				arm[a][4] == arm[b][(i + 2) % 6] && arm[a][5] == arm[b][(i + 1) % 6]))
			return true;
	}
	return false;
}
int main() {
	scanf("%d", &n); long long sum, key;
	for (int i = 0; i<n; i++) {
		for (int j = 0; j<6; j++) {
			scanf("%d", &arm[i][j]);
		}
	}
	for (int i = 0; i<n; i++) {
		sum = 0;
		for (int j = 0; j<6; j++) {
			sum += arm[i][j] * 2;
		}
		key = sum%prime;//关键码
		vector<int>::size_type k;
		for (k = 0; k<hsh[key].size(); k++) {
			if (isTheSame(i, hsh[key][k])) {
				printf("Twin snowflakes found.\n");
				exit(0);//终止程序
			}

		}
		hsh[key].push_back(i);//未找到相同雪花,数据下标加入哈希表中
	}
	printf("No two snowflakes are alike.\n");
}

转载地址:http://www.cnblogs.com/fu11211129/p/4211506.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值