nyoj 130 同样的雪花 【哈希】
同样的雪花
- 描写叙述
- 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;
}
nyoj 130 同样的雪花 【哈希】的更多相关文章
- ascii码所有字符对照表(包含汉字和外国文字)
http://www.0xaa55.com/thread-398-1-1.html看到了0xaa55的这个帖子,想起了2年前我在51cto发的一个帖子http://down.51cto.com/dat ...
- 简答哈希实现 (nyoj 138 找球号2)
例题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=138 代码目的:复习哈希用 代码实现: #include "stdio.h&qu ...
- nyoj 138 找球号(二)(哈希)
题目:nyoj——138 /*** 哈希求解...采用链表保存 插入时,可以去除重复 查找 找到该组,然后在改组的查找 当这个组不存在时或是没有找到时是 NO 其他是YES 1e6+1 时间最短 */ ...
- poj3349找相同的雪花(哈希)
题目传送门 题目大意:给你n个雪花,每个雪花的六个棱都有各自的长度,如果存在两片雪花的每条棱长度对应相同,则输出一句英文,如果不存在就输出另外一句英文,n和长度都比较大. 思路:第一次真正接触哈希,查 ...
- POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 35642 Accep ...
- NYOJ 2356 哈希计划(模拟)
题目链接: http://acm.nyist.me/JudgeOnline/problem.php?id=2356 题目描述 众所周知,LLM的算法之所以菜,就是因为成天打游戏,最近LLM突然想玩&l ...
- NYOJ 138 找球号(二) (哈希)
题目链接 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是&qu ...
- NYOJ 136 等式 (哈希)
题目链接 描述 有以下等式:a1x13+a2x23+a3x33+a4x43+a5*x53=0 x1,x2,x3,x4,x5都就在区间[-50,50]之间的整数,且x1,x2,x3,x4,x5都不等于0 ...
- nyoj 528 找球号(三)(哈希)
点解:题目链接 两种办法,1是使用容器set做 2必须知道这个结论, 突然感觉数论很强大啊,,,, /*//set容器处理 出一次加进去,再出现删掉,这个最后留下的就是那个只出现基数次的 #incl ...
随机推荐
- MSSQL - SqlDataAdapter连接数据库提高性能用法
SqlDataAdapter 与 SqlConnection 和 SqlCommand 一起使用,以便在连接到 SQL Server 数据库时提高性能. SqlDataAdapter 的这一实现自动打 ...
- ASP.NET - 缓存(Cache)
页面缓存: 给页面添加<%@ OutPutCache Duration = “15” VaryByParam = “none” %> 这样就可以启用页面缓存了,那么在规定的时间内,页面之访 ...
- Spring3.0 入门进阶(1):从配置文件装载Bean
Spring 已经盛行多年,目前已经处于3.0阶段,关于Spring的概念介绍性的东西网上已经很多,本系列博客主要是把一些知识点通过代码的方式总结起来,以便查阅. 作为入门,本篇主要介绍Bean的加载 ...
- .Net有许多Office,PDF,Email,HTML的控件
比如: Aspose.Total for .NET includes the following components: Aspose.Words for .NET 16.3.0 (4/13/2016 ...
- 不包含SDK头文件, 补全API定义
/// @file main.cpp /// @brief 不包含SDK头文件, 补全API定义 #ifdef __cplusplus extern "C" { #endif /* ...
- 批处理运行python
@echo off cd C:\test start python test.py start python test2.py exit
- 14.8.2 Verifying File Format Compatibility 校验文件格式兼容性:
14.8.2 Verifying File Format Compatibility 校验文件格式兼容性: 14.8.2.1 Compatibility Check When InnoDB Is St ...
- android 多媒体数据库详解
主要分为几节: 1. Android的媒体文件内部是如何存储的? 2. Andoid的媒体文件如何获取? 3. 在使用媒体文件的一些小技巧. 1. Android的多媒体如何存储的? Android的 ...
- cp命令的实现
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE 4096 ...
- CABasicAnimation学习Demo 包含了一些经常使用的动画效果
个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Cop ...