1145. Hashing - Average Search Time
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 ( 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 1. 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 1.
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
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int isPrime(int n){
int sqr = sqrt(1.0*n);
if(n == || n == )
return ;
for(int i = ; i <= sqr; i++){
if(n % i == )
return ;
}
return ;
}
int hashTB[];
int main(){
int Msize, N, M;
scanf("%d%d%d", &Msize, &N, &M);
while(!isPrime(Msize)){
Msize++;
}
fill(hashTB, hashTB + , -);
for(int i = ; i < N; i++){
int loc, key;
scanf("%d", &key);
loc = key % Msize;
if(hashTB[loc] < ){
hashTB[loc] = key;
}else{
int tag = ;
int q = ;
while(q <= Msize && hashTB[(loc + q*q) % Msize] >= ){
q++;
}
if(hashTB[(loc + q*q)%Msize] < ){
hashTB[(loc + q*q)%Msize] = key;
}else{
printf("%d cannot be inserted.\n", key);
}
}
}
int cnt = ;
for(int i = ; i < M; i++){
int key, loc;
scanf("%d", &key);
loc = key % Msize;
int q = ;
cnt++;
if(hashTB[loc] != key && hashTB[loc] >= ){
while(q <= Msize){
if(hashTB[(loc + q*q) % Msize] != key && hashTB[(loc + q*q) % Msize] >= ){
cnt++;
}else{
cnt++;
break;
}
q++;
}
}
}
double ans = 1.0 * cnt / M;
printf("%.1lf", ans);
cin >> N;
}
总结:
1、题意:先将元素用平方探测法插入哈希表,再进行查找元素并计算平均比较次数。
2、平方探测: loc = key mod Tsize, 当有冲突时依次探测 (loc ± di^2) mod Tsize,di从1到Tsize - 1(本题di从1到Tsize,题目要求with positive increments only,则探测时正向探测,每次仅加di^2,不用减)。当依次找遍所有位置仍没有空位时插入失败。
3、查找次数。 1)查找成功的情况:一次就查找成功,或者在平方探测的过程中查找成功,记录比较次数。2)查找失败:在平方探测的过程中找到了一个空位,则说明失败;或者平方探测的di取遍了它的范围,沿途位置都非空但却没有找到key。记录比较次数。
1145. Hashing - Average Search Time的更多相关文章
- PAT 1145 Hashing - Average Search Time [hash][难]
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of d ...
- [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 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- PAT 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 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 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 ...
- PAT_A1145#Hashing - Average Search Time
Source: PAT A1145 Hashing - Average Search Time (25 分) Description: The task of this problem is simp ...
- PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time PAT-1145 需要注意本题的table的容量设置 二次探测,只考虑正增量 这里计算平均查找长度的方法和书本中的不同 #include&l ...
随机推荐
- IntelliJ IDEA使用教程(非常全面)
这个编辑器我就不再多做介绍了.直接开始新建maven hello world 的Java web项目啦 你电脑上得有jdk1.7,或者1.8,然后就是maven3.x吧,再有就是tomcat7以上吧. ...
- git fetch 和git pull 的差别
1.git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令: git fetch orgin master //将远程仓库的master分支下载到本地当前branch中 git ...
- hive表链接
等值连接 不等职链接 外部链接 没有包含在聚合函数(这里是count)中的列,都需要包含在group by函数中: 正确的外链接的写法,用的是右外链接: 自链接表 把同一张表 看成了2张表
- Django--CRM
一 . 什么是CRM CRM就是客户关系管理系统(customer relationship management) 二 . 用户登录 # models.py文件 class UserProfile( ...
- vscode git设置远程仓库码云
https://www.cnblogs.com/klsw/p/9080041.html
- Mac下安装MySQL(Mac 10.12)
系统:Mac OS 10.12 MySQL:5.7.15 前言: 安装mysql有两种方式:1为官方下载dmg安装包.2为使用brew进行安装. 安装步骤: 一.官方下载dmg安装包进行安装 1.登陆 ...
- 使用脚本调用maven命令后脚本直接退出问题
在带有maven命令的bat脚本执行的时候,执行完一个mvn 目标后会自动退出,pause命令也无效. 原因:mvn本身是一个bat命令,因此在exit退出的时候,整个脚本进程将退出,加入call命令 ...
- Tomcat服务的安装与配置
介绍 百度百科 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开 ...
- Luogu5205 【模板】多项式开根(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! 写NTT把i<<=1写成了i<<=2,又调了一年.发现我的日常就是数组开 ...
- Redis——redis使用redis-dump,redis-load导出导入数据——【三】
来源 https://www.cnblogs.com/dadonggg/p/8662455.html https://blog.csdn.net/chenxinchongcn/article/deta ...