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

相关文章

关于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

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

spoj705( 求不相同的子串个数)

题意:求串s的不同子串的个数 解题思路:任何子串都是某个后缀的前缀,对n个后缀排序,求某个后缀的前缀的个数,减去height[i](第i个后缀与第i-1 个后缀有相同的height[i]个前缀)。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstrin

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al