[POJ3349] 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

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

Sample Output

Twin snowflakes found.

Source

CCC 2007

题意

每个雪花都有六个分支,用六个整数表示,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的。输入仅一组数据,输入多个雪花,判断是否有形状一致的雪花存在,若存在,输出Twin snowflakes found.;否则,输出No two snowflakes are alike.

题解

将雪花拆成顺时针和逆时针,每一种由把一个环拆成两倍环的长度,再就是简单的哈希。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

const int N=1200010,Mod=1200007;
struct node
{
	int num[6],next;
}hash[N];
int cnt,hd[Mod];

void Init()//初始化
{
	cnt=0;
	for(int i=1;i<N;++i) hd[i]=-1;
}

void Insert(int id,int *num)//插入操作
{
	++cnt;
	for(int i=0;i<6;++i) hash[cnt].num[i]=num[i];
	hash[cnt].next=hd[id],
	hd[id]=cnt;
}

int GetHash(int *num)//hash值
{
	int Res=0;
	for(int i=0;i<6;++i) Res+=num[i];
	return Res%Mod;
}

bool Cmp(int *a,int *b)//比较,同1异0
{
	for(int i=0;i<6;++i)
		if(a[i]!=b[i]) return 0;
	return 1;
}

bool Search(int *num)//查找
{
	int id=GetHash(num);
	for(int i=hd[id];i!=-1;i=hash[i].next)
		if(Cmp(num,hash[i].num)) return 1;
	Insert(id,num);
	return 0;
}

int main()
{
	int T,num[2][12];
	Init();
	for(scanf("%d",&T);T;--T)
	{
		for(int i=0;i<6;++i) scanf("%d",num[0]+i),num[0][i+6]=num[0][i];//拆环
		for(int i=0;i<6;++i) num[1][i]=num[1][i+6]=num[0][11-i];//顺时针变逆时针,逆时针变顺时针
		for(int i=0;i<6;++i)
			if(Search(num[0]+i)||Search(num[1]+i))
			{
				puts("Twin snowflakes found.");
				return 0;
			}
	}
	puts("No two snowflakes are alike.");
	return 0;
}

本文作者:OItby @ https://www.cnblogs.com/hihocoder/

未经允许,请勿转载。

原文地址:https://www.cnblogs.com/hihocoder/p/11394652.html