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. SQL Server修改数据库对象所有者(Owner)浅析

    在SQL Server数据库中如何修改数据库对象(表.视图.存储过程..)的所有者(Owner)呢?一般我们可以使用系统提供的系统存储过程sp_changeobjectowner来修改. 我们先看看s ...

  2. [AlwaysOn Availability Groups]排查:AG超过RTO

    排查:AG超过RTO 自动故障转移或者手动转移之后,没有数据都是,你可能会发现切换时间超过了你的RTO.或者当你评估切换时间同步提交secondary副本,发现超过了你的RTO. 1. 通常原因 通常 ...

  3. MapRedue开发实例

    一些例子,所用版本为hadoop 2.6.5 1.统计字数 数据格式如下(单词,频数,以tab分开): A 100 B 97 C 98A 98 package com.mr.test; import ...

  4. apache ab测试命令详解

    这篇文章主要介绍了apache性能测试工具ab使用详解,需要的朋友可以参考下   网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种 ...

  5. 搭建 OpenStack 实验环境 - 每天5分钟玩转 OpenStack(16)

    在学习 OpenStack 各服务之前,让我们先搭建起一个实验环境. 毋庸置疑,一个看得到摸得着而且允许我们随便折腾的 OpenStack 能够提高我们的学习效率. 因为是我们自己学习用的实验环境,C ...

  6. 复制粘贴出来的悲剧----spring实现文件下载和HttpStatus.CREATED

    今天真是被自己的懒惰和复制粘贴给坑惨了... 网上有这么一个spring下载文件的最佳实践: @RequestMapping("download") public Response ...

  7. 理解 OpenStack 高可用(HA)(3):Neutron 分布式虚拟路由(Neutron Distributed Virtual Routing)

    本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...

  8. OpenStack 企业私有云的若干需求(3):多租户和租户间隔离(multi-tenancy and isolation)

    本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...

  9. 全国高校网安联赛Web专场~WriteUp

    1.Sign 题目:Good Luck!flag{X-nuca@GoodLuck!} Flag直接写在题目上了,flag{X-nuca@GoodLuck!} 2.BaseCoding 提示:这是编码不 ...

  10. Win10升级后回退后无法检测新版本的修复办法

    笔记本原来装的是Win10 10240版本,升级到14393版本后进行了回退.回退后,Win10系统再也检测不到新版本更新了.   解决办法如下: 1.打开注册表:HKEY_LOCAL_MACHINE ...