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 84 E. Count The Blocks
传送门: 1327- E. Count The Blocks 题意:给你一个整数n,求10^n内(每个数有前导零)长度为1到n的块分别有多少个.块的含义是连续相同数字的长度. 题解:从n=1开始枚举 ...
- HDU 3336——Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- Codeforces #Round 632 div2 A~C
A. Little Artem Young boy Artem tries to paint a picture, and h ...
- Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums (数学)
题意:给你三个正整数\(x\),\(y\),\(z\),问能够找到三个正整数\(a\),\(b\),\(c\),使得\(x=max(a,b)\),\(y=max(a,c)\),\(z=max(b,c) ...
- Linux系统编程【1】——编写more命令
背景介绍 笔者知识背景 笔者接触Linux快一年了.理论知识方面:学习了操作系统基础知识,了解进程调度.内存分配.文件管理.磁盘I/O这些基本的概念. 实操方面:会使用Linux简单命令,在嵌入式系统 ...
- 恢复win10 LTSC 2019 图片查看器功能
1.开始–运行–输入"regedit"打开注册表. 2. 在打开的注册表编辑器中,从左侧依次展开:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Win ...
- C# 静态类 单例模式 对比
公司的类都需要使用单例模式实现,这个可以节省资源,避免重复对象的生成.但是静态类也可以做到这一点,而且写起来更简洁,于是查阅相关资料,希望弄明白两者的差别. 1.单例模式可以在用到的时候初始化,而静态 ...
- WSL2 使用Docker运行.NET Core
Docker的安装在前面说过了,此处就不说了,我们检查一下版本: 步入正题. 首先,我们为项目创建Dockerfile(无扩展名) 确保Docker是启动状态: 构建镜像,注意名称必须是全部小写(此处 ...
- python3基本数据类型补充
列表 list 有序,可嵌套,可重复,元素可修改 方括号 占用空间小但时间消耗比较大 mylist=["kimi",1,1,1,["amy",18]] V=my ...
- Leetcode(83)-删除排序链表中的重复元素
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: 输入: 1->1->2->3-&g ...