POJ-3349 简单哈希表的应用
2016-10-12 20:42
211 查看
题目大意:
大概就是说,输入n组数据,每组数据6个,如果n组数据中存在两组数据完全相等(数据顺序忽略)比如
1 2 3 4 5 6
3 2 1 4 5 6
这两组数据就是相等的
算法分析:
这道题的数据量很大,如果只是暴力遍历的话,很容易超时。所以可以使用哈希表来查询。哈希表的优点在于查询速度快,缺点在于储存时容易地址冲突。
所以需要优化。
这里我使用的时开散列法,就是说,哈希表储存的节点是链表(数组)。
代码:
#include <iostream> #include <algorithm> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; struct node { int side[6]; }; node hash_[15000][1000]; int top[15000]; // the count of hash_[index] int getKey(node& demo) { long long key = 0; for (int i = 0; i < 6; i++) key += demo.side[i]; key %= 14997; return (int)key; } bool cmp(node first, node second) { sort(first.side, first.side+6); sort(second.side, second.side+6); for (int i = 0; i < 6; i++) { if (first.side[i] != second.side[i]) return false; } return true; } int main() { int n, tmpKey; node tmp; scanf("%d", &n); memset(top, 0, sizeof(top)); for (int i = 0; i < n; i++) { for (int j = 0; j < 6; j++) { scanf("%d", &tmp.side[j]); } tmpKey = getKey(tmp); for (int j = 0; j < top[tmpKey]; j++) { if (cmp(hash_[tmpKey][j], tmp)) { printf("Twin snowflakes found.\n"); return 0; } } hash_[tmpKey][top[tmpKey]++] = tmp; } printf("No two snowflakes are alike.\n"); return 0; }
相关文章推荐
- poj 3630(简单trie应用)
- poj 2406 还是KMP的简单应用
- 最短路径简单应用 畅通工程续 POJ 1874
- POJ 1125 Stockbroker Grapevine【floyd简单应用】
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(素数筛的简单应用)
- C# 哈希表结合动态数组的简单操作和应用
- POJ 3349 Snowflake Snow Snowflakes(哈希表)
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- POJ 2236 Wireless Network 【并查集的简单应用 判断是否在同一连通分量】
- POJ 2398 Toy Storage(叉积的简单应用)
- poj 3630 简单Trie树的应用
- POJ 2752、2406、1961 KMP的next[](或p[])简单应用
- 哈希表的简单应用
- POJ 2492 A Bug's Life【并查集的简单应用同类的判断】
- POJ 1988 Cube Stacking【并查集的简单应用 堆木块】
- poj 3020(匈牙利算法的简单应用,关键是构图)
- poj 3207 Ikki's Story IV - Panda's Trick(2-sat简单应用)
- poj 1611 The Suspects 初级->数据结构->简单并查集的应用
- POJ 1789 Truck History【最小生成树简单应用】
- poj——1789(图论之最小生成树的简单应用)