POJ-3349 简单哈希表的应用
来源:互联网 发布:手机淘宝在哪领优惠券 编辑:程序博客网 时间:2024/06/03 15:51
题目大意:
大概就是说,输入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;}
0 0
- POJ-3349 简单哈希表的应用
- poj 3140 简单的树的应用
- 哈希表的简单应用
- 哈希表的简单应用
- poj 1061 青蛙的约会 扩展gcd的简单应用
- poj 3020(匈牙利算法的简单应用,关键是构图)
- Sliding Window poj 单调队列的简单应用
- POJ 2398 Toy Storage(叉积的简单应用)
- poj 2236 Wireless Network (简单的并查集应用)
- poj 1611 简单并查集的应用
- POJ 1979 Red and Black (DFS的简单应用)
- ACM练级日志:POJ 2318 叉积的简单应用
- poj--3080Blue Jeans KMP的简单应用
- poj 2945 Find the Clones trie树的简单应用
- poj(2318)——TOYS(叉积的简单应用)
- POJ 3250 Bad Hair Day (栈的简单应用)
- [poj 2001] Shortest Prefixes(tire树的简单应用)
- POJ-3253--Fence Repair(简单哈夫曼的应用)
- 《深入理解Android 卷一》第2章 深入理解JNI
- 订单模块
- CSS3高级特性之径向渐变
- jQuery即点即改
- sql中ROW_NUMBER函数的理解
- POJ-3349 简单哈希表的应用
- iOS分类中通过runtime添加动态属性
- Leetcode 187. Repeated DNA Sequences[medium]
- 新手Centos7安装mysql,并使用libzdb数据库链接池连接数据库
- Could not publish server configuration for Tomcat v7.0 Server at localhost. Multiple Contexts have a
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- div隐藏时jquery outerHeight(true);值有问题
- 项目中并发下单的问题怎么解决?
- ajax实现页面异步删除