同样的雪花

时间限制: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. EasyUI - Tree 树组件

    效果: 数据库设计: 使用的数据: 其中的字段,是跟据要生成的树节点的属性定义的. text:代表要显示的字段名称. state:是否是目录节点. iconCls:节点的图标是什么. url:跳转的链 ...

  2. EasyUI - DataGrid 组建 - [ 组件加载和分页 ]

    效果: 原理:通过POST传递到数据后台字段. 此时上传的参数,page:当前页数,rows:每页显示的页数. 有此两项参数,计算取出数据条数. 通过后台接受参数,进行处理并返回抽取的数据. html ...

  3. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  4. Android开发系列(二十八):使用SubMenu创建选项菜单

    大部分手机上边都会有一个"MENU"键,在一个应用安装到手机上之后,能够通过"MENU"显示该应用关联的菜单. 可是,从Android 3.0開始,Androi ...

  5. 暴力拆解CPU

    http://www.ruanyifeng.com/blog/2010/11/cpu_autopsy.htmlhttp://bbs.mydigit.cn/read.php?tid=110272http ...

  6. [置顶] oracle 数据库表中转换成java代码

    --数据库中字段java代码 select col.TABLE_NAME,replace(initcap(col.TABLE_NAME),'_', '')   , 'private '||decode ...

  7. 爬虫总结_java

    基于webmagic的爬虫项目经验小结 大概在1个月前,利用webmagic做了一个爬虫项目,下面是该项目的一些个人心得,贴在这里备份: 一.为什么选择webmagic? 说实话,开源的爬虫框架已经很 ...

  8. ExtJS拖拽效果

    ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...

  9. Firemonkey 自定义Button的Style

    这篇文章模仿HTML中基于CSS的Button,通过Style实现自定义样式的Button. 前言 主要模仿的CSS代码如下: CSS Code 123456789101112131415161718 ...

  10. MFC/VC CxImage 简单配置与使用 (完整版)

    如果本篇文章还不能解决你在生成解决方案以及便宜过程中的问题 请参阅: http://blog.csdn.net/afterwards_/article/details/7997385 我个人配置过来成 ...