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

相关文章

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

SQLServer中生成雪花ID(Snowflake ID)的实现方法

《SQLServer中生成雪花ID(SnowflakeID)的实现方法》:本文主要介绍在SQLServer中生成雪花ID(SnowflakeID)的实现方法,文中通过示例代码介绍的非常详细,... 目录前言认识雪花ID雪花ID的核心特点雪花ID的结构(64位)雪花ID的优势雪花ID的局限性雪花ID的应用场景

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三