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
- 1
- 例子输出
-
- 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 ...
随机推荐
- 使用Eclipse EE开发web项目
最近使用EclipseEE开发web项目,遇到了以下几个问题: 1. 通过tomcat启动web应用的时候,总是提示找不到包或者class. 经过排查,发现所有的jar包并没有放到WEB-INF/li ...
- Apache Thrift的简单使用
Apache Thrift的简单使用 ---------------------- 1. 简介 Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架.它有一个代码生成器来对它所 ...
- 2数组的slice和splice方法
var colors=["blue","red","black","yellow","gray",& ...
- Axure RP中线条的设置
文章来源与网络 来自:非原型不设计
- PHP环境部署问题集合
Forbidden You don't have permission to access /phpmyadmin/ on this server. 安装wamp 后访问 http://localho ...
- <2>集腋成裘
标量项: [root@wx03 2]# cat a1.pl unshift(@INC,"/root/big/2"); use Horse;; print $Horse::days; ...
- Godiva_百度百科
Godiva_百度百科 北京 三里屯 北京市朝阳区三里屯路19号院10号楼一层S10-13单元及二层S10-22单元 100027 北京朝阳大悦城北京市朝阳区朝阳北路101号朝阳大悦城1号商业楼1F- ...
- redhat6.3+oracle11GR2 单库 安装规划
oracle11g单实例安装+redhat6.3 规划 一.查看环境 [root@JSCS78DB dev]# cat /etc/redhat-release Red Hat Enterprise ...
- vc中改变对话框的背景色
---- 笔者曾在<软件报>2000年第5期中讨论过如何改变控件的颜色,但还有相当一部分的读者来信提问:一个基于对话框的MFC AppWizard应用程序中,如何改变对话框的背景颜色呢?对 ...
- javascript(五)验证
<input id="domo" type="text"> <script> function my_function(){ var ...