Snowflake Snow Snowflakes
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 integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n 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 or counterclockwise), 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 Sample Output Source CCC 2007 |
————————————————————开森的分割线————————————————————
思路:又滚来写题解了哦也!Hash。要比较若干长度为6的字符串之中是否有两个一样(环形:顺时针、逆时针)将每种字符串映射一下,采用开散列法,即发生冲撞的元素挂到一个链上。开一个数组table[H][ ],存各个字符串以及其哈希值,之后每输入一个雪花,得到它的哈希值,判重,然后存入哈希表。
如何得到哈希值?求和、之后取余一个大质数(看情况)。如何判重?取其哈希值,对链上所有元素进行匹配,顺时针逆时针各走一遍。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int H = 9997;
const char s_not[] = {"No two snowflakes are alike."};
const char s_yes[] = {"Twin snowflakes found."};
int chain[H];
struct Snow
{
int a[6], hash_val;
}table[H][10];
int get_hash(Snow& cur)
{
cur.hash_val = 0;
for(int i = 0; i < 6; i++)
cur.hash_val += cur.a[i];
return cur.hash_val %= H;//求和取余法
}
bool match(Snow& cur, Snow& tab)
{
bool twin_cw, twin_ancw;//顺时针和逆时针
for(int i = 0; i < 6; i++) {//枚举起点,找到匹配的起点
if(cur.a[0] == tab.a[i]) {
twin_cw = twin_ancw = true;
for(int j = 1; j < 6; j++) {//顺时针走一遍、逆时针走一遍
if(cur.a[j] != tab.a[(i+j)%6])
twin_cw = false;
if(cur.a[j] != tab.a[(i-j+6)%6])
twin_ancw = false;
}
if(twin_cw || twin_ancw)
return true;
}
}
return false;
}
bool find_tab(Snow& cur)
{
int& tmp = cur.hash_val;//取出哈希值
for(int i = 0; i < chain[tmp]; i++) {
if(match(cur, table[tmp][i]))//在链上判重
return true;
}
return false;
}
int main()
{
int n;
scanf("%d", &n);
Snow flake;
bool twin = false;
for(int i = 0; i < n; i++) {
for(int j = 0; j < 6; j++)
scanf("%d", &flake.a[j]);
if(twin)
continue;//减枝,如果已发现重复,就不必任何操作
int& val = flake.hash_val;
val = get_hash(flake);//先得到哈希值
if(find_tab(flake))
twin = true;//判重,注意!一定要在存入哈希表之前判重不然肯定重
table[val][chain[val]++] = flake;//存入哈希表
}
if(twin)
puts(s_yes);
else
puts(s_not);
return 0;
}