PAT-1145(Hashing - Average Search Time)哈希表+二次探测解决冲突
Hashing - Average Search Time
PAT-1145
- 需要注意本题的table的容量设置
- 二次探测,只考虑正增量
- 这里计算平均查找长度的方法和书本中的不同
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
using namespace std;
const int maxn=10004;
int table[100*maxn];
int size,n,m;
bool isPrime(int n){
if(n<=1){
return false;
}
for(int i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
int findPrime(int n){
while(!isPrime(n)){
n++;
}
return n;
}
bool insertHash(int n){
int ori=n;
bool flag=false;
for(int j=0;j<size;j++){
if(table[(n+j*j)%size]==-1){
table[(n+j*j)%size]=ori;
flag=true;
break;
}
}
return flag;
}
int findHash(int n){
//返回次数
int ans=0;
for(int j=0;j<=size;j++){
ans++;
if(table[(n+j*j)%size]==-1||table[(n+j*j)%size]==n)
break;
}
return ans;
}
int main(){
memset(table,-1,sizeof(table));
cin>>size>>n>>m;
size=findPrime(size);
for(int i=0;i<n;i++){
int a;
cin>>a;
if(!insertHash(a)){
cout<<a<<" cannot be inserted."<<endl;
}
}
int sums=0;
for(int i=0;i<m;i++){
int b;
cin>>b;
sums+=findHash(b);
}
printf("%.1lf\n",sums*1.0/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 1145 Hashing - Average Search Time
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 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] 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 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 甲级 1145 Hashing - Average Search Time
https://pintia.cn/problem-sets/994805342720868352/problems/994805343236767744 The task of this probl ...
- 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 ...
随机推荐
- Educational Codeforces Round 95 (Rated for Div. 2) B. Negative Prefixes (贪心,构造)
题意:给你一串长度为\(n\)的序列,有的位置被锁上了,你可以对没锁的位置上的元素任意排序,使得最后一个\(\le0\)的前缀和的位置最小,求重新排序后的序列. 题解:贪心,将所有能动的位置从大到小排 ...
- Codeforces Round #654 (Div. 2) D. Grid-00100 (构造)
题意:构造一个\(n\)x\(n\)只含\(0\)和\(k\)个\(1\)的矩阵,统计每一行每一列\(1\)的sum,然后构造一个权值最大行和最小行的差的平方加权值最大列和最小列的差的平方的最小和(\ ...
- C# ArrayList和List的区别
ArrayList存的是object对象,可以装任何类型,但涉及装箱拆箱,效率低,类型转换可能报错 List只能存一种类型,不涉及装箱拆箱,效率高 总结:一般情况用List吧
- 7.PowerShell DSC之模式
DSC两种模式 DSC有两种模式,Push模式和Pull模式 Push模式 基本流程 写配置--编译生成mof--推送到目标服务器,由目标服务器LCM执行mof并进行指定的配置 优点 架构简单.成本低 ...
- 【非原创】codeforces 1029F Multicolored Markers 【贪心+构造】
题目:戳这里 题意:给a个红色小方块和b个蓝色小方块,求其能组成的周长最小的矩形,要求红色或蓝色方块至少有一个也是矩形. 思路来源:戳这里 解题思路:遍历大矩形可能满足的所有周长,维护最小值即可.需要 ...
- python3基本数据类型补充
列表 list 有序,可嵌套,可重复,元素可修改 方括号 占用空间小但时间消耗比较大 mylist=["kimi",1,1,1,["amy",18]] V=my ...
- Ubuntu-16.04下Docker通过阿里云镜像安装(apt-get)
由于通过官方路径安装docker时总是连接不上,所以从网上找了半天,通过阿里云镜像安装docker,我的Linux是ubuntu-16.04 一.配置源里的阿里云镜像仓库 sudo vim /etc/ ...
- 如何在 macOS 上进行滚动截屏
如何在 macOS 上进行滚动截屏 Shift-Command-5 https://support.apple.com/zh-cn/guide/mac-help/mh26782/mac demo Xn ...
- Array.fill & String.padStart & String.padEnd
Array.fill & String.padStart & String.padEnd Array.fill arr.fill(value[, start[, end]]) http ...
- Service Worker in Action
Service Worker in Action https://caniuse.com/#feat=serviceworkers Service Workers 1 W3C Candidate Re ...