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 ...
随机推荐
- 关于js中遍历总结
1.for循环 var arr = []; for (var i = 0; i < arr.length; i++) { if (条件1) return; if (条件2) break; if ...
- 用css制作星级评分
Step 1: XHTML <ul class="star-rating"> <li><a href="#" titl ...
- springmvc传递有特殊字符的路径参数
因为hostKey这里是IP(例如127.0.0.1)包含了特殊字符. 实际传递到后台的是127.0.0少了一截 @GetMapping("/metrics/jobId/{jobId}/{ ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- configChanges
android中的组件Activity在manifest.xml文件中可以指定参数android:ConfigChanges,用于捕获手机状态的改变. 在Activity中添加了android:con ...
- kotlin 遇到的问题
转载请表明 https://i.cnblogs.com/EditPosts.aspx?opt=1 从5月18号goole正式公布用kotlin做为android的新语言,做为android也很庆幸可以 ...
- IT 运行在云端,而云运行在 Linux 上
导读 IT 正在逐渐迁移到云端.那又是什么驱动了云呢?答案是 Linux. 当连微软的 Azure 都开始拥抱 Linux 时,你就应该知道这一切都已经改变了.不管你接不接受, 云正在接管 IT 已经 ...
- nginx的allow和deny配置
转自:http://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/ 单看nginx模块名ngx_http_access_module ...
- 06.Curator Barrier
分布式Barrier是这样一个类: 它会阻塞所有节点上的等待进程,知道某一个被满足, 然后所有的节点继续进行. 比如赛马比赛中, 等赛马陆续来到起跑线前. 一声令下,所有的赛马都飞奔而 ...
- 160601、Websocet服务端实现
今天是六一儿童节,祝愿小朋友们节日快乐!大朋友们事事顺心! Websocet服务端实现 WebSocketConfig.Java ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...