PAT 1145 Hashing - Average Search Time [hash][难]
1145 Hashing - Average Search Time (25 分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 104. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 105.
Output Specification:
For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted.
where X
is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.
Sample Input:
4 5 4
10 6 4 15 11
11 4 15 2
Sample Output:
15 cannot be inserted.
2.8
题目大意:就是用二次探查法解决冲突问题。
//这个题给我干懵了。为啥查询15时要多+1次?好奇怪啊。
学习了哈希中解决冲突的几种办法:
1.二次探查法
首先h=hash(x)=x%maxSize;
探查需要:j在[0.maxSize-1]这个区间内,使用公式:
new=(h+j^2)%maxSize;
在查询时:
如果查到一个=-1也就是没有这个数,那么就停止;
如果j已经到了maxSize-1仍旧没有查到,那么就是未出现在哈希表里。
//不过真的不明白为什么这里要多加1次。
并且正常的探查是需要左右同时进行的,形如1*1.-1*1,2*2,-2*2.....以此类推。
代码转自:https://blog.csdn.net/qq_34594236/article/details/79814881
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; bool isPrime(int num) {
if (num < ) return false;
for (int i = ; i <= sqrt(num); i++) {
if (num % i == ) return false;
}
return true;
} int H(int key, int TSize){
return key % TSize;
} int msize, n, m, a, table[];
int main() {
memset(table, -, sizeof(table));
scanf("%d%d%d", &msize, &n, &m); while (isPrime(msize) == false) msize++; for (int i = ; i < n; i++) {
scanf("%d", &a); bool founded = false;
for (int j = ; j < msize; j++) {
int d = j * j;
int tid = (H(a, msize) + d) % msize;
if (table[tid] == -) {
founded = true;
table[tid] = a;
break;
}
}
if (founded == false) {
printf("%d cannot be inserted.\n", a);
}
}
int tot = ; for (int i = ; i < m; i++) {
scanf("%d", &a);
int t = ;
bool founded = false;
for (int j = ; j < msize; j++) {
tot++;
int d = j * j;
int tid = (H(a, msize) + d) % msize;
if (table[tid] == a || table[tid] == -) { // 找到或者不存在
founded = true;
break;
}
}
if(founded ==false) {
tot++;
}
} printf("%.1f\n", tot*1.0/m); return ;
}
//真是学习了。
还有一个非常重要的问题,关于段的,就是定义的数组的长度,如果输入是10000,那么将其转换为最近的素数,那只能是10007,所以最好定义数组长度为10010.
PAT 1145 Hashing - Average Search Time [hash][难]的更多相关文章
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT Advanced 1145 Hashing – Average Search Time (25) [哈希映射,哈希表,平⽅探测法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- 1145. Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...
- 1145. Hashing - Average Search Time (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
随机推荐
- op bug 修复计划
省-市-区-组-成员 多了一个组的下拉框,说不清它和区的联系 把它删掉,它的点击事件(把组的id和内容传给隐形的text,text通过ajax提交到数据库)给区下面的组
- 假设A.jsp内设定一个<jsp:useBean>元素:
假设A.jsp内设定一个<jsp:useBean>元素: <jsp:useBean id=”bean1” class=”myBean” /> 下列哪一个为真?(选择1项) A. ...
- 【BZOJ】1664: [Usaco2006 Open]County Fair Events 参加节日庆祝(线段树+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1664 和之前的那题一样啊.. 只不过权值变为了1.. 同样用线段树维护区间,然后在区间范围内dp. ...
- 怎么用ChemDraw连接两个结构片段
作为最新版的ChemOffice 15.1的核心组件,ChemDraw几乎能解决所有平面化学结构中的绘制问题.如果用户想连接两个分开的结构片段,ChemDraw提供两种连接两个化学结构片段的方法,分别 ...
- redhat6.4 zabbix3.0.2安装
zabbix不用说了,很好的服务器监控管理工具,还支持中文哈! 1.添加epel仓库,有更多可用的软件包 rpm -ivh http://download.fedoraproject.org/pub/ ...
- elasticsearch性能因素总结
一:硬件方面 在预算充足的情况下.特别是一些高并发业务的搜索.硬件层面占用整个elasticsearch性能空间很大比例. 1)内存: 单实例的情况下,尽量分配32G,排序和统计都是以及内存计算的 ...
- js实现二分搜索法
二分搜索法: 也称折半搜索,是一种在有序数组中查找特定元素的搜索算法. 实现步骤: 1. 首先从数组中间开始查找对比,若相等则找到,直接返回中间元素的索引. 2. 若查找值小于中间值,则在小于中间值的 ...
- .NET开发过程中的全文索引使用技巧之Solr
前言:相信许多人都听说过.net开发过程中基于Lucene.net实现的全文索引,而Solr是一个高性能,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查 ...
- 如何使用 awk 复合表达式
导读 一直以来在查对条件是否匹配时,我们使用的都是简单的表达式.那如果你想用超过一个表达式来查对特定的条件呢?本文中,我们将看看如何在过滤文本和字符串时,结合多个表达式,即复合表达式,用以查对条件. ...
- Codeforces Round #186 (Div. 2).D
纠结的一道dp. 状态转移方程还是比较好想的,优化比较纠结 D. Ilya and Roads time limit per test 3 seconds memory limit per test ...