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. python爬虫—爬取百度百科数据

    爬虫框架:开发平台 centos6.7 根据慕课网爬虫教程编写代码 片区百度百科url,标题,内容 分为4个模块:html_downloader.py 下载器 html_outputer.py 爬取数 ...

  2. ORA-19563: header validation failed for file

    在测试服务器还原数据库时遇到了ORA-19563错误.如下所示 RMAN-00571: ======================================================== ...

  3. springmvc学习资料整理

    springmvc接口开发返回XML/JSON数据 1.SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上):http: ...

  4. Oracle用户密码过期和用户被锁解决方法

    [原因/触发因素] 确定是由于oracle11g中默认在default概要文件中设置了"PASSWORD_LIFE_TIME=180天"所导致. [影响和风险] 影响    密码过 ...

  5. Java报表工具FineReport导出EXCEL的四种API

    在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出.分页分sheet导出和大数据量导出.对于excel 2003版,由于限制了每个sheet的最大行数和列数,大数据量导出 ...

  6. 374&375. Guess Number Higher or Lower 1&2

    做leetcode的题 We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You ...

  7. 微信公共号的PHP开发(基础篇)——玩一下

    最近没事儿开了个微信号,写点东西给家人啊什么的看,你们想看的话就这个嘛: 然后就意外的看到imooc上的微信公众号开发相关了.每天科研的累累的,做点这个不是很累的东西吧. 微信公共号开发 一.基础 1 ...

  8. 并查集补集作法 codevs 1069 关押罪犯

    1069 关押罪犯 2010年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description ...

  9. Qt 之 饼图

    Qt 库由许多模块组成,其中的 Qt Charts,包含了一系列图表组件. 1  饼图 (pie chart) 1.1  Charts 模块 .pro 中添加如下语句: QT += charts 头文 ...

  10. git stash 用法

    git stash用于将当前工作区的修改暂存起来,就像堆栈一样,可以随时将某一次缓存的修改再重新应用到当前工作区. 一旦用好了这个命令,会极大提高工作效率.   直接举例说明: 1.准备工作,首先初始 ...