Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 39324   Accepted: 10298

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.

思路

简单哈希题,用分离链接表解决冲突,另外用邻接表代替链表,为了将地址冲突降低到最小,这里取了10n(n = 100000)中最大的素数。输入雪花的信息,求出其key值,若在hash[]中还没出现,直接添加,否则说明此前出现过key值相同的雪花,因为key值相同的雪花的信息保存在一个链表中,所以遍历链表判断是否存在两片相同的雪花。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod = 999983;
const int maxn = 100005;
int tot = 0,head[mod+5],num[maxn][6],next[maxn];

int IndexHash(int id)
{
	int hashval = 0;
	for (int i = 0;i < 6;i++)	hashval = (hashval%mod+num[id][i]%mod)%mod;
	return hashval%mod;
}

bool cmp(int id1,int id2)
{
	bool flag;
	for (int i = 0;i < 6;i++)    //顺时针比较雪花的各个角
	{
		flag = true;
		for (int st = i,j = 0; j < 6;j++,st = (st+1) == 6?0:st+1)
		{
			if (num[id1][st] != num[id2][j])
			{
				flag = false;
				break;
			}
		}
		if (flag)	return true;
	}

	for (int i = 0;i < 6;i++)   //逆时针比较雪花的各个角
	{
		flag = true;
		for (int st = i,j = 5;j >= 0;j--,st = (st+1)==6?0:(st+1))
		{
			if (num[id1][st] != num[id2][j])
			{
				flag = false;
				break;
			}
		}
		if (flag)	return true;
	}
	return false;
}

void addnode(int hashval)
{
	next[tot] = head[hashval];
	head[hashval] = tot++;
}

bool solve(int id)
{
	int hashval = IndexHash(id);
	for (int i = head[hashval]; i != -1;i = next[i])
	{
		if (cmp(i,id))	return true;
	}
	addnode(hashval);
	return false;
}

int main()
{
	int N;
	while (~scanf("%d",&N))
	{
		memset(head,-1,sizeof(head));
		bool flag = false;
		for (int i = 0;i < N;i++)
		{
			for (int j = 0;j < 6;j++)	scanf("%d",&num[i][j]);
			if (flag)	continue;
			flag = solve(i);
		}
		if (flag)	printf("Twin snowflakes found.\n");
		else	printf("No two snowflakes are alike.\n");
	}
	return 0;
}

  

POJ 3349 Snowflake Snow Snowflakes(简单哈希)的更多相关文章

  1. poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   Accep ...

  2. [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30512   Accep ...

  3. POJ 3349 Snowflake Snow Snowflakes(哈希)

    http://poj.org/problem?id=3349 题意 :分别给你n片雪花的六个角的长度,让你比较一下这n个雪花有没有相同的. 思路:一开始以为把每一个雪花的六个角的长度sort一下,然后 ...

  4. POJ 3349 Snowflake Snow Snowflakes(哈希表)

    题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes fou ...

  5. POJ 3349 Snowflake Snow Snowflakes (哈希表)

    题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...

  6. POJ - 3349 Snowflake Snow Snowflakes (哈希)

    题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花.若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相 ...

  7. 哈希—— POJ 3349 Snowflake Snow Snowflakes

    相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions ...

  8. POJ 3349 Snowflake Snow Snowflakes

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...

  9. POJ 3349 Snowflake Snow Snowflakes (Hash)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 48646   Accep ...

随机推荐

  1. .NET Framework各版本比较

    (1)本文比较了.NET Framework多个版本之间的区别,方便各位选择和切换.NET Framework. 版本号 发布日期 Visual Studio的版本 Windows上的默认情况 CLR ...

  2. YourSQLDba 配置——修改备份路径

    公司一直使用YourSQLDba做本地备份,磁带机将本地备份文件上带做异地容灾备份.近期整理.验证备份时发现本地备份目录命名五花八门 其中有历史原因,也有无规划化的缘故,看着这些五花八门的目录,越看越 ...

  3. php之验证码小程序

    验证码功能(个人理解): 减轻服务器的压力(如12306的验证码功能): 防止暴力注册 个人思路:在a-z,A-Z,1-9生成n位随机的数来构成新的验证码. 关于生成验证码的几个小函数 range() ...

  4. class.c 添加中文注释(2)

    /* Class Device Stuff */ int class_device_create_file(struct class_device * class_dev, const struct ...

  5. sql server几种读写分离方案的比较

    在生产环境中我们经常会遇到这种情况: 前端的oltp业务很繁忙,但是需要对这些运营数据进行olap,为了不影响前端正常业务,所以需要将数据库进行读写分离. 这里我将几种可以用来进行读写分离的方案总结一 ...

  6. Centos下yum安装PHP

    添加 yum 源 Centos 6.x 的源 # rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6 ...

  7. 【2016-11-6】【坚持学习】【Day21】【主窗口关闭时,同步关闭它的子窗口】

    本来想用委托实现的.但是又觉得没有必要. 方法如下: public MainWindow() { InitializeComponent(); this.Closing += MainWindow_C ...

  8. 图像处理工具V1.0

    图像处理工具V1.0(仿彗星图片处理工具.VS2015安装界面)----个人无聊作品 以下是界面: 部分代码一.(摘自网络----加水印代码): public static void ImageWat ...

  9. [No000041]如果你被ruby惯坏了,不如试试python3-在Windows下安装ipython

    说明:我比较喜欢绿色软件,因此,下载的是python3为zip包.所以执行命令时请注意当前路径. 先安装pip (请去官网下载get-pip.py 地址: https://pip.pypa.io/en ...

  10. sql 首写字母查询姓名(字段)

    来自网上大神,不知道是谁,挂不上链接 /////////////////////// 1.生成方法函数 create function f_GetPy(@str nvarchar(4000)) ret ...