PAT 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 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 10^4. 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 10^5.
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> //hash表, 平方探测
#include<vector>
using namespace std;
bool isprime(int a){
if(a<=1) return false;
for(int i=2; i*i<=a; i++)
if(a%i==0) return false;
return true;
}
int main(){
int n, m, msize;
cin>>msize>>n>>m;
while(!isprime(msize)) msize++;
vector<int> t(msize, 0);
for(int i=0; i<n; i++){
int a, flag=0;
cin>>a;
for(int j=0; j<msize; j++){
int pos=(j*j+a)%msize;
if(t[pos]==0){
flag=1;
t[pos]=a;
break;
}
}
if(flag==0) printf("%d cannot be inserted.\n", a);
}
double aver=0.0;
for(int i=0; i<m; i++){
int a;
cin>>a;
int cnt=1;
for(int j=0; j<msize; j++){
int pos=(j*j+a)%msize;
if(t[pos]==a||t[pos]==0)
break;
cnt++;
}
aver+=cnt;
}
printf("%.1f\n", aver/m);
return 0;
}
PAT 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 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 ...
- 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 ...
- 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 ...
随机推荐
- Jquery ajax json 值回传不了
今天调试系统的时候,MVC 框架下调用ajax 值,回传的json值获取不到,后来发现竟然是服务没开,郁闷不已,留个截图,做个纪念.
- Book-MySQL-Operate
创建数据库 CREATE DATABASE db_name 查看数据库 SHOW DATABASES 选择数据库 USE db_name 删除数据库 DROP DATABASE db_name 列主键 ...
- vue seo
最近在实习,刚来没几天,老大没安排什么大事给我,昨天下午说给我一个小任务,要求如下: 1.收集几个流量大的网站(必须是vue做的)页面交互和逻辑尽可能复杂多样2.对比一下各个页面的seo是如何做的3. ...
- extjs grid禁止表格头部使用鼠标拖拽改变宽度
extjs6 经典版 表格头部使用鼠标拖动 禁止改变列的宽度 只需要给grid 设置属性enableColumnResize:false就可以啦 xtype:'grid', enableColumnR ...
- curl 做爬虫 用服务器代理ip
有时候会ip会封锁,所以会用一些模拟代理ip进行抓取测试 从网上找了一下代码 function curl_string ($url,$user_agent,$proxy){ $ch = ...
- c++ gets函数
函数名称:gets函数 函数结构:gets() 所需头文件:#include<cstdio> 函数作用:持续读入,直到遇到换行停止输出.
- php 文件上传限制修改
修改PHP上传文件大小限制的方法 1. 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完.但在php中,默认的该页最久执行时间为 30 秒.就是说超过30秒,该脚本就停止 ...
- 递推DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...
- Java泛型Object和?区别
在写spark streamming读取kafka latest offset的时候,有一下语句: Map<TopicAndPartition, Object> latestOffsets ...
- 提取header头进行模块化处理
在进行爬取网上东西的时候一般网站都做了UA的过滤,解决办法就是在代码中加入. 所以才有了本篇提取header头信息单独写成一个模块或者说是函数/类的想法,直接上示例 1.把UA头信息在浏览器中复制出来 ...