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. MySQL修改root账号密码

    MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法   1: 使用set password语句修改 mysql> select user(); +----- ...

  2. MySQL如何导出带日期格式的文件

    一网友问在MySQL中如何只用SQL语句导出带日期格式的文件.觉得有点意思,于是尝试了一下.导出文件使用SELECT INTO OUTFILE 但是OUTFILE后面的值不能使用变量,所以只能使用动态 ...

  3. asp.net signalR 专题—— 第一篇 你需要好好掌握的实时通讯利器

    一:背景 我们知道传统的http采用的是“拉模型”,也就是每次请求,每次断开这种短请求模式,这种场景下,client是老大,server就像一个小乌龟任人摆布, 很显然,只有一方主动,这事情就没那么完 ...

  4. Ubuntu下的生活--安装

    概要 整个安装过程是通过离线包安装,而非在线安装. 目录 JDK安装与配置 Eclipse安装与配置 Apache安装与配置 MySQL的安装 JDK安装与配置 版本:jdk-7u71-linux-i ...

  5. 使用ansible编译安装运维工具tmux

    实验系统:CentOS 6.6_x86_64 实验前提:提前准备好编译环境,防火墙和selinux都关闭 软件介绍:tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD, ...

  6. 1、策略模式(Strategy)

    //抽象接口 class ReplaceAlgorithm { public: ; }; //三种具体的替换算法 class LRU_ReplaceAlgorithm : public Replace ...

  7. Python+selenium自动化脚本编辑过程中遇到的问题和小技巧

    应该也不算是问题和技巧,算是实践中学习到的Python,记录下,也不定时更新 1.通过截取url判断 实例: self.assertEqual(self.broswer.current_url[sel ...

  8. 洛谷11月月赛round.2

    P3414 SAC#1 - 组合数 题目背景 本题由世界上最蒟蒻最辣鸡最撒比的SOL提供. 寂月城网站是完美信息教室的官网.地址:http://191.101.11.174/mgzd . 题目描述 辣 ...

  9. c++ const函数是什么意思

    c++ const函数是什么意思 只读类型 const修饰在类的成员函数的尾部,表示这个函数不会修改类的成员.相当于this指针是const的.

  10. enumerate用法

    Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup ...