同样的雪花

时间限制: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.
来源
POJ
上传者
张云聪

题意:给定多组包括6组数的序列,推断当中是否有两组是相等的,相等的根据是这两组数正序匹配或者逆序匹配。

题解:以每组的和为key对每片雪花进行哈希,然后从和相等的雪花里进行匹配。

#include <stdio.h>
#include <string.h>
#include <vector> #define maxn 100001
#define MOD 100001 int n;
struct Node {
int a[6];
} snow[maxn];
std::vector<int> f[MOD]; void getData() {
int i, j, sum;
memset(f, 0, sizeof(f));
scanf("%d", &n);
for(i = 0; i < n; ++i) {
for(j = sum = 0; j < 6; ++j) {
scanf("%d", &snow[i].a[j]);
sum += snow[i].a[j];
}
f[sum % MOD].push_back(i);
}
} bool Judge(int x, int y) {
int i, j, count;
for(i = 0; i < 6; ++i) {
if(snow[x].a[i] == snow[y].a[0]) {
for(count = j = 0; j < 6; ++j)
if(snow[x].a[(i+j)%6] == snow[y].a[j])
++count;
if(count == 6) return true; for(count = j = 0; j < 6; ++j)
if(snow[x].a[((i-j)%6+6)%6] == snow[y].a[j])
++count;
if(count == 6) return true;
}
}
return false;
} bool test(int k, int m) {
int i, j;
for(i = 0; i < m; ++i)
for(j = i + 1; j < m; ++j)
if(Judge(f[k][i], f[k][j]))
return true;
return false;
} void solve() {
int i, j, k, ok = 0;
for(i = 0; i < MOD; ++i)
if(!f[i].empty() && test(i, f[i].size())) {
ok = 1; break;
}
printf(ok ? "Twin snowflakes found.\n"
: "No two snowflakes are alike.\n");
} int main() {
// freopen("stdin.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
getData();
solve();
}
return 0;
}

然后又尝试了下链表:结果TLE

#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define maxn 100001 int n;
struct Node {
int a[6];
Node *next;
};
struct Node2 {
int sum;
Node2 *nextNode2;
Node *next;
} root; void insertNode(Node2 *n2p, Node tmp) {
Node *p = (Node *) malloc(sizeof(Node));
*p = tmp; p->next = n2p->next;
n2p->next = p;
} void insertNode2(int sum, Node tmp) {
Node2 *p = root.nextNode2, *q = &root;
while(true) {
if(!p || p->sum > sum) {
Node2 *temp = (Node2 *) malloc(sizeof(Node2));
temp->sum = sum; temp->nextNode2 = p;
temp->next = NULL; q->nextNode2 = temp;
insertNode(temp, tmp);
return;
} else if(p->sum == sum) {
insertNode(p, tmp);
return;
}
q = p; p = p->nextNode2;
}
} void destoryNode(Node *p) {
Node *q;
while(p) {
q = p; p = p->next;
free(q);
}
} void destoryNode2(Node2 *p) {
Node2 *q;
while(p) {
destoryNode(p->next);
q = p; p = p->nextNode2;
free(q);
}
} void getData() {
int i, j, sum; Node tmp;
destoryNode2(root.nextNode2);
root.nextNode2 = NULL;
root.next = NULL;
root.sum = -1;
scanf("%d", &n);
for(i = 0; i < n; ++i) {
for(j = sum = 0; j < 6; ++j) {
scanf("%d", &tmp.a[j]);
sum += tmp.a[j];
}
insertNode2(sum, tmp);
}
} bool JudgeArr(int a[], int b[]) {
int i, j, cnt;
for(i = 0; i < 6; ++i) {
if(a[i] == b[0]) {
for(j = cnt = 0; j < 6; ++j)
if(a[(i+j)%6] == b[j]) ++cnt;
if(cnt == 6) return true; for(j = cnt = 0; j < 6; ++j)
if(a[(i-j+6)%6] == b[j]) ++cnt;
if(cnt == 6) return true;
}
}
return false;
} bool JudgeList(Node *pn) {
Node *p1, *p2;
for(p1 = pn; p1; p1 = p1->next) {
for(p2 = p1->next; p2; p2 = p2->next)
if(JudgeArr(p1->a, p2->a)) return true;
}
return false;
} void solve() { Node *np;
for(Node2 *n2p = root.nextNode2; n2p; n2p = n2p->nextNode2) {
if(JudgeList(n2p->next)) {
printf("Twin snowflakes found.\n");
return;
}
}
printf("No two snowflakes are alike.\n");
} int main() {
// freopen("stdin.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
getData();
solve();
}
return 0;
}

NYOJ130 同样的雪花 【Hash】的更多相关文章

  1. nyoj-130-相同的雪花(hash)

    题目链接 /* Name:NYOJ-130-相同的雪花 Copyright: Author: Date: 2018/4/14 15:13:39 Description: 将雪花各个分支上的值加起来,h ...

  2. 相同的雪花 Hash

    相同的雪花 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You may have heard that no two snowflakes are alike. ...

  3. nyoj130 相同的雪花

    相同的雪花 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 You may have heard that no two snowflakes are alike. ...

  4. POJ 3349 HASH

    题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...

  5. 0x14 hash

    被虐爆了 cry 我的hash是真的菜啊... poj3349 肝了一个上午心态崩了...一上午fail了42次我的天,一开始搞了个排序复杂度多了个log,而且是那种可能不同值相等的hash,把12种 ...

  6. POJ 3349 Snowflake Snow Snowflakes(哈希表)

    题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes fou ...

  7. 【hash表】收集雪花

    [哈希和哈希表]收集雪花 题目描述 不同的雪花往往有不同的形状.在北方的同学想将雪花收集起来,作为礼物送给在南方的同学们.一共有n个时刻,给出每个时刻下落雪花的形状,用不同的整数表示不同的形状.在收集 ...

  8. Acwing:137. 雪花雪花雪花(Hash表)

    有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,…,ai,6ai,1,ai,2,…,ai,6. 因为雪花的形状是封闭的环形,所以 ...

  9. POJ 3349:Snowflake Snow Snowflakes(数的Hash)

    http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K T ...

随机推荐

  1. HDU 4907 Task schedule

    对于询问q 假设q不存在直接输出q 否则输出后面第一个不存在的数 从2*10^5到1遍历一边ac #include<bits/stdc++.h> using namespace std; ...

  2. UIActivityIndicatorView-初识IOS

    UIActivityIndicatorView是一个加载动画的视图,一般加载一个网页页面之前会经常用到. 上一个随笔,我讲到了页面加载的页面的那些代理方法 - (void) viewWillAppea ...

  3. mongodb.open失效导致访问地址404

    今天做编辑文章功能的时候发现一个问题,编辑并保存完成后再次跳转到当前文章所在的地址,结果报404,打断点发现查询数据库的时候mongodb.open方法失效.百度后找到了原因: 编辑保存的时候打开了数 ...

  4. asp.net mvc 页面缓存

    在任务中需要实现点击浏览器back按钮,加载的前一页面需要强制刷新. 想要在前端通过js来绑定数据实现,但是觉得太麻烦,还是用另一种方式来解决: 不缓存该页面. 简单易懂: Response.Cach ...

  5. arraylist与List<>

    arraylist 不用规定 类型 list<>用规定类型 推荐list 这个编译检查类型  出错率低 代码质量高

  6. RouteHttpMap要添加的引用

    System.Web.Routing.RouteCollection' does not contain a definition for 'MapHttpRoute' 此错的解决方式是添加 Syst ...

  7. .net 4.0 面向对象编程漫谈基础篇读书笔记

    话说笔者接触.net 已有些年头,做过的项目也有不少,有几百万的,也有几十万的,有C/S的,也有B/S的.感觉几年下来,用过的框架不少,但是.net的精髓一直没有掌握.就像学武之人懂得各种招式,但内功 ...

  8. 控制反转IOC与依赖注入DI【转】

    转自:http://my.oschina.net/1pei/blog/492601 一直对控制反转.依赖注入不太明白,看到这篇文章感觉有点懂了,介绍的很详细. 1. IoC理论的背景我们都知道,在采用 ...

  9. hdu 2191多重背包

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  10. HTML5画布(线条、渐变)

    绘制直线时,一般会用到moveTo与lineTo两种方法. 案例1: <!DOCTYPE html><html><head lang="en"> ...