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 ...
随机推荐
- fzu2200 cleaning
Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距离不能为2,他们想知道共有多少种方法. In ...
- Eclipse无法打开提示could not open jvm.cfg错误
先言:我的问题只是出在之前我的java_jdk原来安装在F:\Temp这里,但是我之后在整理文件的时候把这个文件夹改成了F:\java_jdk.所以导致了错误的出现 解决方法1: 因为我的只是目录名称 ...
- Strategic game POJ - 1463 树型dp
//题意:就是你需要派最少的士兵来巡查每一条边.相当于求最少点覆盖,用最少的点将所有边都覆盖掉//题解://因为这是一棵树,所以对于每一条边的两个端点,肯定要至少有一个点需要放入士兵,那么对于x-&g ...
- EFCore学习记录--数据访问技术人门
1.安装Microsoft.EntityFrameworkCore.Sqlite.Microsoft.EntityFrameworkCore.Tools包2.创建模型 数据库上下文模型:Bloggin ...
- Kafka官方文档V2.7
1.开始 1.1 简介 什么是事件流? 事件流相当于人体的中枢神经系统的数字化.它是 "永远在线 "世界的技术基础,在这个世界里,业务越来越多地被软件定义和自动化,软件的用户更是软 ...
- nmap进阶使用[脚本篇]
nmap 进阶使用 [ 脚本篇 ] 2017-05-18 NMAP 0x01 前言 因为今天的重点并非nmap本身使用,这次主要还是想给大家介绍一些在实战中相对比较实用的nmap脚本,所以关于 ...
- μC/OS-III---I笔记5---多值信号量
多值信号量 操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统如何实现进程之间的同步和互斥显得极为重要.比如对同一部分资源的访问是要互斥,不能在另一个进程A在访 ...
- Dell Display Manager for Mac
Dell Display Manager for Mac DDM for macOS solution https://www.dell.com/community/Monitors/DDM-for- ...
- Bearer Token & OAuth 2.0
Bearer Token & OAuth 2.0 access token & refresh token http://localhost:8080/#/login HTTP Aut ...
- How to create a folder symbol link in macOS
How to create a folder symbol link in macOS macOS 创建文件夹链接 Make AliasMake Alias Symbolic Links 符号链接 $ ...