您的位置:首页 > 其它

poj_3349 Snowflake Snow Snowflakes(数字哈希)

2016-10-30 11:58 387 查看
Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 39366 Accepted: 10311
DescriptionYou 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 snowflakehas 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.InputThe 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 sixintegers (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.OutputIf 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.
数字哈希,每次读入一片雪花时把雪花的所有情况都存进哈希表中,哈希表的链地址用类似向前星的方法实现。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1200010#define mod 1200007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{int val[6];int next;};int n;int hashTable[maxn];Node node[maxn];int cot;void initHash(){cot = 0;memset(hashTable, -1, sizeof(hashTable));}bool cmp(int *a, int *b){for(int i = 0; i < 6; i++) if(a[i] != b[i]) return false;return true;}int getHash(int *a){int key = 0;for(int i = 0; i < 6; i++){key += a[i];}return key%mod;}void insertHash(int* val, int key){for(int i = 0; i < 6; i++) node[cot].val[i] = val[i];node[cot].next = hashTable[key];hashTable[key] = cot++;}bool searchHash(int *val){int key = getHash(val);int next = hashTable[key];while(next != -1){if(cmp(val, node[next].val)) return true;next = node[next].next;}insertHash(val, key);return false;}int main(){while(~scanf("%d", &n)){bool flag = 0;initHash();int starCount[2][12];for(int i = 0; i < n; i++){for(int j = 0; j < 6; j++){scanf("%d", &starCount[0][j]);starCount[1][11-j] = starCount[1][5-j] = starCount[0][j+6] = starCount[0][j];}if(flag) continue;for(int j = 0; j < 6; j++){if(searchHash(starCount[0] + j) || searchHash(starCount[1] + j)){flag = 1;break;}}}if(flag) printf("Twin snowflakes found.\n");else printf("No two snowflakes are alike.\n");}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: