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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • html超链接位置怎么改,如何修改HTML超链接样式?

    html超链接位置怎么改,如何修改HTML超链接样式?在网页开发中,我们不免会用到超链接,将内容链接到原网页上。如果不对超链接进行设置,链接默认以固定样式显示,过于单一。那么我们要如何修改HTML中的超链接呢?这篇文章W3Cschool小编为大家介绍一下。我们都知道,超链接是用标签来显示的。如果我们需要修改样式,则需要通过CSS修改它的样式。标签的样式还分为四个类型,分别为未访问、已访问、鼠标滑过、点击。a:link:未被访问的链接a:v…

    2022年7月19日
    29
  • SpringBoot启动一下就停止了_win10安装boot运行失误

    SpringBoot启动一下就停止了_win10安装boot运行失误springboot启动失败原因:本文想做一个系统管理,在springboot下进行开发,但是提交代码时出现启动失败,上网搜索发现各种原因主要包括:1说@EnableAutoConfiguration注解没加.2说@SpringBootApplication注解没加3说springboot-中包含tomcat疑问,删除maven依赖,重新下载解决’4说spring-boot-starter-parent依赖冲突,删除一个即可5说启动类要和项目在同一级下

    2025年10月14日
    2
  • NFS(网络文件系统)简介及搭建

    NFS(网络文件系统)简介及搭建

    2021年6月2日
    111
  • 我的世界显示服务器领地指令,我的世界领地指令介绍 我的世界领地指令怎么设置…

    我的世界显示服务器领地指令,我的世界领地指令介绍 我的世界领地指令怎么设置…在我的世界这款经典有趣的建造类游戏中,为了让自己的领地不然其他玩家占用,我们可以设置一下领地。那我的世界领地怎么设置呢?下面是小编给大家分享的我的世界领地指令大全,大家赶紧来了解一下吧!一、我的世界设置领地:先用一块木头斧子左键敲击一方块设置点A,右键敲击一方块设置点B(可以输入“/resselectsize”查看所选区域的大小);之后输入“/rescreate123”(例)这样设置后,就…

    2022年9月23日
    3
  • springboot 配置JedisPool 简洁有效 复制即可运行「建议收藏」

    springboot 配置JedisPool 简洁有效 复制即可运行「建议收藏」吐槽一下,本来以为随便找个文章跟着配置一下,就可以了,后来发现好多例子无法运行。估计是环境的问题,后来把大神们的例子综合一下,终于配置出一个简洁有效的例子,个人太懒,技术太烂,复杂的代码不理解,所以能简就简。抛砖引玉,大家多指点。

    2025年9月14日
    7
  • 竞争性需求分析

    竞争性需求分析

    2021年11月18日
    48

发表回复

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

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