同样的雪花

时间限制: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;
}

nyoj 130 同样的雪花 【哈希】的更多相关文章

  1. ascii码所有字符对照表(包含汉字和外国文字)

    http://www.0xaa55.com/thread-398-1-1.html看到了0xaa55的这个帖子,想起了2年前我在51cto发的一个帖子http://down.51cto.com/dat ...

  2. 简答哈希实现 (nyoj 138 找球号2)

    例题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=138 代码目的:复习哈希用 代码实现: #include "stdio.h&qu ...

  3. nyoj 138 找球号(二)(哈希)

    题目:nyoj——138 /*** 哈希求解...采用链表保存 插入时,可以去除重复 查找 找到该组,然后在改组的查找 当这个组不存在时或是没有找到时是 NO 其他是YES 1e6+1 时间最短 */ ...

  4. poj3349找相同的雪花(哈希)

    题目传送门 题目大意:给你n个雪花,每个雪花的六个棱都有各自的长度,如果存在两片雪花的每条棱长度对应相同,则输出一句英文,如果不存在就输出另外一句英文,n和长度都比较大. 思路:第一次真正接触哈希,查 ...

  5. POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 35642   Accep ...

  6. NYOJ 2356 哈希计划(模拟)

    题目链接: http://acm.nyist.me/JudgeOnline/problem.php?id=2356 题目描述 众所周知,LLM的算法之所以菜,就是因为成天打游戏,最近LLM突然想玩&l ...

  7. NYOJ 138 找球号(二) (哈希)

    题目链接 描述 在某一国度里流行着一种游戏.游戏规则为:现有一堆球中,每个球上都有一个整数编号i(0<=i<=100000000),编号可重复,还有一个空箱子,现在有两种动作:一种是&qu ...

  8. NYOJ 136 等式 (哈希)

    题目链接 描述 有以下等式:a1x13+a2x23+a3x33+a4x43+a5*x53=0 x1,x2,x3,x4,x5都就在区间[-50,50]之间的整数,且x1,x2,x3,x4,x5都不等于0 ...

  9. nyoj 528 找球号(三)(哈希)

    点解:题目链接 两种办法,1是使用容器set做 2必须知道这个结论,  突然感觉数论很强大啊,,,, /*//set容器处理 出一次加进去,再出现删掉,这个最后留下的就是那个只出现基数次的 #incl ...

随机推荐

  1. ibatis通过Map封装参数调用存储过程

    一.存储过程如下(领导写的) CREATE OR REPLACE PROCEDURE agent_UIMOrIMEICheck_pro ( I_CARD_NO IN VARCHAR2, --UIM卡或 ...

  2. SharePoint 2013 &quot;通知我&quot;简单的功能

    简单的功能 "通知我"内部列表或文档库中的主要项目.加入/删除/修改等操作,用户的E- mail通知设定功能:设置列表或文档库通知的能力,有可能设置通知为一个单一的项目.这是Sha ...

  3. Swift - 添加、修改、删除通讯录联系人

    使用AddressBook.framework框架,我们除了可以很方便的获取通信录里的联系人.同时,还能对通讯录进行新增.修改.删除联系人操作. (注意:这些操作同查询一样,首先需要发起授权请求) 1 ...

  4. ubuntu下使用charles代理

    charles 最新的版本是3.10,但是这个版本还没有license可以用,所以使用3.9.2版本. 解压缩包就可以用了. 如果只是代理http请求,只要设置: Proxy -> Proxy ...

  5. PHP学习之-面向对象

    PHP学习之-面向对象 1.什么是对象 "世界万物皆对象",一切可以被抽象出来的东西都是对象.像花,草.看不到的"概念"都是对象. 2.对象的基本组成 a.属性 ...

  6. 基于visual Studio2013解决C语言竞赛题之1078打印日历

        题目 解决代码及点评 /************************************************************************/ /* ...

  7. Round Numbers

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1301472836 大致题意: 输入两个十进制正整数a和b,求闭区间 [a ,b] 内有多少 ...

  8. MSSQL - SQL Server2008附加数据库失败 错误号:5120

    附加数据库时,显示错误,错误信息为 一种解决方法为,设置mdf文件所在文件夹的权限(有些资料说只设置mdf文件的权限就好,但我试了不管用),在文件夹上右击——属性——安全,如图所示: 选择组或用户名中 ...

  9. 用scponly限制只能拷文件,不能登陆(MAC版)

    目的: 限制用户在特定目录(不能看到上级或者根目录) 只能执行scp或者sftp拷贝特别目录下的文件 不能SSH登陆,其它命令不能执行   机制: SSH登陆成功后,scponly会接管SHELL,并 ...

  10. JSP中两种include的区别

    首先说明这两种都是什么: <%@ include file=”relativeURI”%> 可以叫作静态include(静态包含),是jsp指令中的一种,(JSP指令控制JSP编译器如何去 ...