1897: 相同的雪花(哈希表入门)

2024-02-07 15:58
文章标签 相同 入门 哈希 雪花 1897

本文主要是介绍1897: 相同的雪花(哈希表入门),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1897: 相同的雪花

时间限制: 1 Sec  内存限制: 64 MB
提交: 11  解决: 7
您该题的状态:已完成
[提交][状态][讨论版]

题目描述

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.

样例输入

<span style="color:#333333"><span style="color:black">1
2
1 2 3 4 5 6
4 3 2 1 6 5</span></span>

样例输出

<span style="color:#333333"><span style="color:black">Twin snowflakes found.</span></span>

 代码中包含两种方法

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int MAXN=100005;
const int MOD=5000;
int snow[MAXN][6];
vector<int>Hash[MOD];
bool isSame(int a, int b) {int i,j,k;for(i=0;i<6;i++){for(j=0;j<6;j++){if(snow[a][j]!=snow[b][(i+j)%6]){break;}}if(j==6) return true;for(j=0;j<6;j++){if(snow[a][j]!=snow[b][(i+6-j)%6]){break;}} if(j==6) return true;}return false;/*if(//顺时针方向(snow[a][0] == snow[b][i] &&snow[a][1] == snow[b][(i+1)%6] &&snow[a][2] == snow[b][(i+2)%6] &&snow[a][3] == snow[b][(i+3)%6] &&snow[a][4] == snow[b][(i+4)%6] &&snow[a][5] == snow[b][(i+5)%6])|| //逆时针方向(snow[a][0] == snow[b][(i+5)%6] &&snow[a][1] == snow[b][(i+4)%6] &&snow[a][2] == snow[b][(i+3)%6] &&snow[a][3] == snow[b][(i+2)%6] &&snow[a][4] == snow[b][(i+1)%6] &&snow[a][5] == snow[b][(i+0)%6]))return true;*/
}
int T,N,ok;
int t;
int main()
{cin>>T;
//	scanf("%d",&T);while(T--){ok=0;cin>>N;for(int i=0;i<MOD;i++)//哈希表初始化 {Hash[i].clear();}for(int i=0;i<N;i++)  //输入数值 {int tot=0;for(int j=0;j<6;j++)//输入过程中并且计算其中的和 {cin>>t;tot+=t;snow[i][j]=t; //将和输入相应的二维数组 }tot=tot%MOD;    //取模,防止和过大 Hash[tot].push_back(i); //将相应的和的顺序输入 }for(int i=0;i<MOD;i++){if(ok)break;if(Hash[i].size()<2)continue;for(int j=0;j<Hash[i].size()-1;j++){for(int k=j+1;k<Hash[i].size();k++){if(isSame(Hash[i][j],Hash[i][k])){ok=1;break;}}}}if(ok)printf("Twin snowflakes found.\n");else printf("No two snowflakes are alike.\n");}return 0;
}

 

这篇关于1897: 相同的雪花(哈希表入门)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/688214

相关文章

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

Java如何将文件内容转换为MD5哈希值

《Java如何将文件内容转换为MD5哈希值》:本文主要介绍Java如何将文件内容转换为MD5哈希值的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java文件内容转换为MD5哈希值一个完整的Java示例代码代码解释注意事项总结Java文件内容转换为MD5

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin