哈希—— POJ 3349 Snowflake Snow Snowflakes
相应POJ题目:点击打开链接
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 33595 | Accepted: 8811 |
Description
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.
Input
The first line of input 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.
Output
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.
Sample Input
2
1 2 3 4 5 6
4 3 2 1 6 5
Sample Output
Twin snowflakes found.
题意:
一片雪花有6片叶子,给出n片雪花,下面每行6个数字。分别代表每片叶子长度。问是否存在有2片雪花形态是同样的。
形态同样的定义:例如以下图,2 3 4 5 6 1 和 4 5 6 1 2 3 和 3 2 1 6 5 4 都是形态同样的雪花,即是同一片雪花。
即是一片雪花能够从某一个数開始顺时针或逆时针数。
思路:
把一片雪花的全部长度相加 mod 一个大质数(100w左右)作为键值key,那就仅仅有key同样的雪花才有可能是形态同样的。
而key同样的雪花会映射到哈希表的同一个槽,那我们用链表把key值同样的在同一个槽连起来。仅仅有key相等的时候就一个个比較key值那条链的雪花是否存在形态同样的,假设有就标记,以后就仅仅是读入数据不比較;假设没有就把雪花增加链表。
也能够採用开方性寻址的方法。思路是一样的。比較雪花形态是否相等要分顺时针和逆时针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
#define N 100007
const int prime = 999983; typedef struct
{
int key;
int arm[7];
}Point; Point *slot[prime+10]; bool Check(Point *p, Point *q) //p是新的,q是旧的
{
int i, j;
//顺时针
for(j = 0; j < 6; j++){
for(i = 0; i < 6; i++)
if(q->arm[i] != p->arm[(i+j)%6]) break;
if(6 == i) return true;
}
//逆时针
for(j = 0; j < 6; j++){
for(i = 0; i < 6; i++){
if(q->arm[i] != p->arm[(12-i-j)%6]) break;
}
if(6 == i) return true;
}
return false;
} int Hash(int k)
{
/*
char s[20];
sprintf(s, "%d", k);
int i, h = 0, a = 232;
for(i = 0; i < strlen(s); i++)
h = (a * h + s[i] - '0') % prime;
*/
return (k<<2);
} bool Try_to_insert(Point *p)
{
int k = p->key;
if(NULL == slot[k]){ //有空槽
slot[k] = p;
return true;
}
while(slot[k])
{
if(Check(p, slot[k])) return false;
k = (k + Hash(k)) % prime;
}
slot[k] = p;
return true;
} int main()
{
//freopen("in.txt","r",stdin);
int i, j, n, flag = 0;
Point *p;
scanf("%d", &n);
for(i = 0; i < n; i++){
p = (Point *)malloc(sizeof(Point));
p->key = 0;
for(j = 0; j < 6; j++){
scanf("%d", &p->arm[j]);
p->key = (p->key + p->arm[j]) % prime;
}
if(flag) continue;
if(!Try_to_insert(p)) flag = 1;
}
if(flag) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
return 0;
}
哈希—— POJ 3349 Snowflake Snow Snowflakes的更多相关文章
- POJ 3349 Snowflake Snow Snowflakes(简单哈希)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 39324 Accep ...
- poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30529 Accep ...
- [ACM] POJ 3349 Snowflake Snow Snowflakes(哈希查找,链式解决冲突)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30512 Accep ...
- POJ 3349 Snowflake Snow Snowflakes
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27598 Accepted: ...
- POJ 3349 Snowflake Snow Snowflakes (Hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 48646 Accep ...
- POJ 3349 Snowflake Snow Snowflakes(哈希)
http://poj.org/problem?id=3349 题意 :分别给你n片雪花的六个角的长度,让你比较一下这n个雪花有没有相同的. 思路:一开始以为把每一个雪花的六个角的长度sort一下,然后 ...
- POJ 3349 Snowflake Snow Snowflakes (哈希表)
题意:每片雪花有六瓣,给出n片雪花,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同) 思路:如果直接遍历会超时,我试过.这里要用哈希表,哈希表的关键码key用六瓣花瓣 ...
- POJ 3349 Snowflake Snow Snowflakes(哈希表)
题意:判断有没有两朵相同的雪花.每朵雪花有六瓣,比较花瓣长度的方法看是否是一样的,如果对应的arms有相同的长度说明是一样的.给出n朵,只要有两朵是一样的就输出有Twin snowflakes fou ...
- POJ - 3349 Snowflake Snow Snowflakes (哈希)
题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花.若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相 ...
随机推荐
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- STL心得
熟悉c++版算法竞赛程序框架 理解变量引用的原理 熟练掌握string和stringstream 熟练掌握c++结构体的定义和使用,包括构造函数和静态成员变量 了解常见的可重载运算符,包括四则运算,赋 ...
- Codeforces Round 251 (Div. 2)
layout: post title: Codeforces Round 251 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- poj2778(AC 自动机)
poj2778 题意 构造只包含 \(A, T, C, G\) 的字符串,且满足不出现指定的一些字符串,问长度为 \(n\) 的字符串有多少种 ? 分析 AC 自动机 + 矩阵快速幂的神题 ,知识点很 ...
- 差分+树状数组【p4868】Preprefix sum
Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前 ...
- 51nod 循环数组最大子段和(动态规划)
循环数组最大子段和 输入 第1行:整数序列的长度N(2 <= N <= 50000) 第2 - N+1行:N个整数 (-10^9 <= S[i] <= 10^9) 输出 输 ...
- 51nod 编辑距离问题(动态规划)
编辑距离问题 给定两个字符串S和T,对于T我们允许三种操作:(1) 在任意位置添加任意字符(2) 删除存在的任意字符(3) 修改任意字符 问最少操作多少次可以把字符串T变成S? 例如: S= “AB ...
- 27、Django实战第27天:全局搜索功能开发
当我们选择其中一个类别(公开课,课程讲师,授课老师),输入搜索内容,点击搜索后会跳转到相应的列表页中进行展示 我们输入的内容作为参数keyword传入后台 搜索"公开课" 当课程中 ...
- KVC与KVO的不同
vc 就是一种通过字符串去间接操作对象属性的机制, 访问一个对象属性我们可以 person.age 也可以通过kvc的方式 [person valueForKey:@"age&quo ...