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 ...
随机推荐
- jQuery EasyUI 选项卡面板tabs使用实例精讲
1. 对选项卡面板区域 div 设置 class=”easyui-tabs” 2. 对选项卡面板区域添加多个 div,每个 div 就是一个选项卡(每个面板一定设置 title) 3. 设置面板 fi ...
- MBG逆向工程报错:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver
修改pom文件,逆向工程如下: <!-- 逆向工程 --> <plugin> <groupId>org.mybatis.generator</groupId& ...
- python之路--JavaScript
一. JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,希望这门语言能成为国际化标准,于是决定将Jav ...
- scrapy全站爬取拉勾网及CrawSpider介绍
一.指定模板创建爬虫文件 命令 创建成功后的模板,把http改为https 二.CrawSpider源码介绍 1.官网介绍: 这是用于抓取常规网站的最常用的蜘蛛,因为它通过定义一组规则为跟踪链接提供了 ...
- LDOOP设置关联后超出新起一页LinkNewPage
关联打印的时候,top,left关联位置是相对于被关联打印项的偏移值,具体可查看本博客相关介绍博文:LODOP打印控件关联输出各内容 正常情况下,超文本超过打印项高度,或纸张高度会自动分页,如果超文本 ...
- 学习 Spring (十七) Spring 对 AspectJ 的支持 (完结)
Spring入门篇 学习笔记 @AspectJ 的风格类似纯 java 注解的普通 java 类 Spring 可以使用 AspectJ 来做切入点解析 AOP 的运行时仍旧是纯的 Spring AO ...
- HTML条件注释
前面的话 IE条件注释是微软从IE5开始就提供的一种非标准逻辑语句,作用是可以灵活的为不同IE版本浏览器导入不同html元素.很显然这种方法的最大好处就在于属于微软官方给出的兼容解决办法而且还能通过W ...
- Luogu4726 【模板】多项式指数函数(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! #include<iostream> #include<cstdio> ...
- SPOJ705-New Distinct Substrings-后缀数组
计算所都不相同子串的个数,做法是所有子串的个数减去sigma(height[]).其中height数组的和便是所有相同子串的个数. 注意 N×(N+1)/2会爆int!但是最终答案在int内.所以使用 ...
- UESTC1013-我的魔法栈-模拟/排列组合
有一个串,有黑色和白色两种元素.一次操作可以把最上面的白色元素变成黑色,同时把这个元素上面的所有元素变成白色. 给你一个30以内的串,计算变成全黑时,元素变化的总和. 我用的方法比较笨,打表处理了1- ...