nyoj 130 同样的雪花 【哈希】

nyoj 130 同样的雪花 【哈希】

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

同样的雪花

时间限制:
1000 ms  |  内存限制:
65535 KB
难度:
4

描写叙述
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.

输入
The first line of the input will contain a single interger T(0<T<10),the number of the test cases.

The first line of every test case 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.
输出
For each test case,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.
例子输入
1
2
1 2 3 4 5 6
4 3 2 1 6 5
例子输出
Twin snowflakes found.

题意:雪花有六个角,分别赋给他们长度,依照顺时针输入,问你在输入的雪花中有没有全然一样的.

分析:依照传统的做法时间是O(n^2),由于数据非常大所以说会超时,要换一种方法,要用到散列表(大神们讲的非常具体,我就现丑了)。

这道题的比較也蛮奇特的。

代码1(链表形式):

#include <cstdio>
#include <cstring>
#define M 20005
using namespace std;
struct node
{
	int a[6];
	struct node *next;
	/* data */
};
node *s[M];

int match(int *temp, int sum){
	int i, j;
	node *p; p = s[sum]->next;
	while(p){
		for(i = 0; i < 6; ++ i){
			for(j = 0; j < 6; ++ j){
				if(temp[j] != p->a[(i+j)%6]) break;
			}
			if(j == 6) return true;
			for(j = 0; j < 6; ++ j){
				if(temp[j] != p->a[(i+6-j)%6]) break;
			}
			if(j == 6) return true;
		}
		p = p->next;
	}
	p = new node;
	for(i = 0; i < 6; ++ i) p->a[i] = temp[i];
	p->next = s[sum]->next;
	s[sum]->next = p;
	return false;
}
int main(){
	int t, n, i, j, temp[6];
	scanf("%d", &t);
	while(t --){
		int sum,flag = 0;
		scanf("%d", &n);
		for(i = 0; i < M; ++ i){
			s[i] = new node; s[i]->next = NULL;
		}
		while(n --){
			sum = 0;
			for(i = 0; i < 6; ++i){
				scanf("%d", &temp[i]);
				sum += temp[i];
			}
			sum %= M;
			if(!flag){
				if(match(temp, sum)) flag = 1;
			}
		}
		if(flag) puts("Twin snowflakes found.");
		else puts("No two snowflakes are alike.");
	}
	return 0;
}        

代码2(三维数组):

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 20000
int s[M][100][6];
int len[M];

int match(int *a, int *b){
	int i, j, k;
	for(i = 0; i < 6; i ++){
		for(j = 0; j < 6; j ++){
			if(a[j] != b[(i+j)%6]) break;
		}
		if(j == 6) return true;
		for(j = 0; j < 6; j ++){
			if(a[j] != b[(i+6-j)%6]) break;
		}
		if(j == 6) return true;
	}
	return false;
}
int main(){
	int t, n, sum, temp[6];
	scanf("%d", &t);
	while(t --){
		int i, j, k, flag = 0;
		scanf("%d", &n);
		memset(len, 0, sizeof(int)*(M+1));
		while(n --){
			sum = 0;
			for(i = 0; i < 6; i ++){
				scanf("%d",&temp[i]);
				sum += temp[i];
			}
			sum %= M;
			for (i = 0; i < 6; ++i){
				s[sum][len[sum]][i] = temp[i];
			}
			++len[sum];
		}
		for(i = 0; i < M; i ++){
			if(len[i] >1)
			for(j = 0; j < len[i]-1; j ++){
				for(k = j+1; k < len[i]; k ++){
					if(match(s[i][j], s[i][k])){
						flag = 1; 
						 break;
					}
				}
				if(flag) break;
			}
			if(flag) break;
		}
		if(flag) puts("Twin snowflakes found.");
		else puts("No two snowflakes are alike.");
	}
	return 0;
}
        

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118043.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【Python】python面试题

    【Python】python面试题一些Python面试题注:本面试题来源于网络,部分内容摘自http://www.cnblogs.com/goodhacker/p/3366618.html1.(1)python下多线程的限制以及

    2022年7月5日
    25
  • .net 平台常用控件

    .net 平台常用控件分布式缓存框架:MicrosoftVelocity:微软自家分布式缓存服务框架。Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度。Redis:是一个高性能的K

    2022年7月2日
    28
  • pycharm的安装选项_pycharm安装后无解释器

    pycharm的安装选项_pycharm安装后无解释器pycharm(windows)安装及其设置中文菜单1.下载在官网(http://www.jetbrains.com/pycharm/download/#section=windows)进行下载

    2022年8月5日
    4
  • Android手机上使用Socks5全局代理-教程+软件

    Android手机上使用Socks5全局代理-教程+软件前言:在Android上使用系统自带的代理,限制灰常大,仅支持系统自带的浏览器。这样像QQ、飞信、微博等这些单独的App都不能使用系统的代理。如何让所有软件都能正常代理呢?ProxyDroid这个软件能帮你解决!使用方法及步骤如下:一、推荐从GooglePlay下载ProxyDroid,目前最新版本是v2.6.6。二、对ProxyDroid进行配置(基本配置:)…

    2022年6月24日
    610
  • 键盘钩子入门

    键盘钩子入门1钩子   钩子是操作系统消息处理的一种机制。通过钩子,应用程序可以安装一个钩子回调过程让系统调用,从而监视系统中的消息队列。在这些消息到达目标窗口之前对这些消息进行处理。1.1钩子函数1)钩子函数会降低操作系统的性能,因为它增加系统处理每一个消息的开销。所以用户除非必须才要安装钩子,而且还要尽可能早地去除钩子。2)操作系统支持多种类型的钩子,每种类型都提供了它特有的消息处理机制。3)对…

    2022年4月28日
    155
  • python库之threading

    Thismoduleconstructshigher-levelthreadinginterfacesontopofthelowerlevelpython库之_threadmo

    2021年12月29日
    33

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号