本文主要是介绍哈希—— 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!