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 ...
随机推荐
- QT在构造函数中退出程序
原地址:http://www.tuicool.com/articles/RZnYze 在QT的界面类的构造过程中,如果想退出整个程序,暴力的做法是调用exit(-1)进行,另外一种不是那么暴力的方式如 ...
- cocos2d-x 精灵移动
在HelloWorldScene.h中声明 class HelloWorld : public cocos2d::CCLayer { public : ...... CCPoin ...
- ArcGIS制图——多图层道路压盖处理
[1] 启动ArcMap,载入各道路要素类.生成名为All-Roads的图层组,如他所看到的,符号级别绘制将对图层组内图层符号起作用: 图层组设置符号等级 [2] 在内容列表中,右 ...
- Unity 3D 文件导入出错
Unity 3D 文件导入出错 安装unity 时我选择了free版的,打开已有项目时出现例如以下错误提示 解决的方法: 先把要导入的文件先复制到unity3d安装文件夹下相应的文件夹内,之后再返回u ...
- javascript json格式解析方法
json.parse用于从一个字符串中解析出json对象 stringify()用于从一个对象解析出字符串 var dataObj = eval("("+json+")& ...
- [IOS]Setting Bundle + StoryBoard
用storyboard添加一个导航栏,其中首页有一个switch,与setting联动,还有一个button,使用modal连接另一个viewControl,其上也有一个按钮,按下销毁本viewCon ...
- IT忍者神龟之中的一个句sql语句——连接同一字段的全部值
Oracle能够用SYS_CONNECT_BY_PATH字符串聚合函数: SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH(productname, ', ')), ', ') ...
- Taking Pictures Using FireMonkey Interfaces
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Taking_Pictures_Using_FireMonkey_Interfaces
- VC/MFC 当鼠标移到控件上时显示提示信息
VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...
- 细说UI线程和Windows消息队列
在 Windows应用程序中,窗体是由一种称为“ UI线程( User Interface Thread)”的特殊类型的线程创建的. 首先, UI线程是一种“线程”,所以它具有一个线程应该具有的所有特 ...