PAT 1145 1078| hashing哈希表 平方探测法
pat 1145:

Quadratic probing (with positive increments only) is used to solve the collisions.:平方探测法解决冲突
哈希表:H(key)求余数、二次平方探测法解决冲突、求平均查找长度AVL = 所有次数和/n
需要注意点:处理冲突统计查找次数时,如果查找到哈希表最后一个也失败了,那么次数要+1.
#include<bits/stdc++.h>
using namespace std;
/*
哈希表:H(key)求余数、二次平方探测法解决冲突、求平均查找长度AVL = All/n
*/
const int maxn = 1e5+5;
int Tsize,n,m;
int a[maxn];
int b[maxn];
int hashTable[maxn];
bool isPrime(int x){
if(x < 2) return false;
for(int i=2;i<=sqrt(x);i++){
if(x%i == 0) return false;
}
return true;
}
int HashKey(int key){
return key%Tsize;
}
int main(){
memset(hashTable,-1,sizeof(hashTable));
cin>>Tsize>>n>>m;
while(isPrime(Tsize) == false) Tsize++;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<m;i++) cin>>b[i];
//插入
for(int i=0;i<n;i++){
bool flag = false;
for(int j=0;j<Tsize;j++){
int idx = (HashKey(a[i]) + j*j) % Tsize;//平方探测法解决冲突
if(hashTable[idx] == -1){
hashTable[idx] = a[i];
flag = true;
break;
}
}
if(flag == false){
printf("%d cannot be inserted.\n",a[i]);
}
}
//查找
int cnt = 0;
for(int i=0;i<m;i++){
bool flag = false;
for(int j=0;j<Tsize;j++){
cnt++;
int idx = (HashKey(b[i]) + j*j) % Tsize;//平方探测法解决冲突 查找下标
if(hashTable[idx] == b[i] || hashTable[idx] == -1 ){
flag = true;
break;
}
}
if(flag == false) cnt++;
}
printf("%.1f",cnt*1.0/m);
return 0;
}
pat 1078 同上
数组存hash表、hash函数求余、平方探测法解决冲突,并且首先哈希表长度为素数。
#include<bits/stdc++.h>
using namespace std;
int Tsize,n;
const int maxn = 1e4+10;
int a[maxn];
int table[maxn];
bool isPrime(int x){
if(x < 2) return false;
for(int i=2;i<=sqrt(x);i++){
if(x%i == 0) return false;
}
return true;
}
int getHash(int key){
return key%Tsize;
}
int main(){
cin>>Tsize>>n;
for(int i=1;i<=n;i++) cin>>a[i];
while(isPrime(Tsize) == false) Tsize++;
bool first = true;
for(int i=1;i<=n;i++){
bool search = false;
for(int j=0;j<Tsize;j++){
int hashIdx = (getHash(a[i]) + j*j)%Tsize;
if(table[hashIdx] == 0){
if(first) {
cout<<hashIdx;
first = false;
}
else cout<<" "<<hashIdx;
search = true;
table[hashIdx] = a[i];
break;
}
}
if(search == false){
if(first){
first = false;
cout<<"-";
}
else{
cout<<" -";
}
}
}
return 0;
}
另补充哈希冲突的处理方法 和 装填因子:


PAT 1145 1078| hashing哈希表 平方探测法的更多相关文章
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT甲级1078 Hashing【hash】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 题意: 给定哈希表的大小和n个数,使用 ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT Advanced 1078 Hashing (25) [Hash ⼆次⽅探查法]
题目 The task of this problem is simple: insert a sequence of distinct positive integers into a hash t ...
- PAT 甲级 1078 Hashing
https://pintia.cn/problem-sets/994805342720868352/problems/994805389634158592 The task of this probl ...
- SDUT 3377 数据结构实验之查找五:平方之哈希表
数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...
- 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甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- PAT 1078 Hashing[一般][二次探查法]
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
随机推荐
- docker上启动nginx,并配置修改nginx的配置文件
1.使用docker 下载nginx 镜像 docker pull nginx 2.启动nginx docker run --name nginx -p 80:80 -d nginx 这样就简单的把 ...
- 物理像素[设备像素] & 逻辑像素[CSS像素];
为什么移动端CSS里面写了1px,实际上看起来比1px粗 了解设备物理像素和逻辑像素的同学应该很容易理解,其实这两个px的含义其实是不一样的, UI设计师要求的1px是指设备的物理像素1px,而CSS ...
- navicat使用教程-PJ
navicat使用教程-PJ Navicat Keygen Patch是一款专门针对Navicat系列软件出品的一款小巧实用,功能强大的破解辅助工具.它能够实现对Navicat全系列版本进行激活破解, ...
- 监控ckeditor内容变化,删除编辑器内图片,ueditor同样适用
let body = document.querySelector("iframe").contentDocument.body; let observer = new Mutat ...
- SQL Server字符串函数STUFF的使用
前言: 最近有个实践练习是将学生报名数据表student中的[st_id]字段的第二个和第三个字符删除,然后在此位置插入新的字符串“200900”,其实就是替换的意思,生成新的字符串. STUFF 函 ...
- windows 安装使用jupyter及 基础配置
jupyter 是什么Jupyter Notebooks 是一个交互式笔记本,支持运行 40 多种编程语言,它的本质是一个 开源的 Web 应用程序,我们可以将其用于创建和共享代码与文档,他可以支持实 ...
- java web工程的配置文件
java web工程的配置文件 1.工程(源码依赖管理) 2.代码生成管理: 3.会话管理:servlet: 4.应用管理: 5.(分布式)资源管理:数据.数据库连接等. pom:源码管理工具 位置: ...
- 用redis-dump工具对redis集群所有数据进行导出导入
安装redis-dump redis-dump是基于ruby开发,需要ruby环境,而且新版本的redis-dump要求2.2.2以上的ruby版本,centos中yum只能安装2.0版本的ruby. ...
- python3报'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)解决方法
运行前指定export PYTHONIOENCODING为utf-8 如 export PYTHONIOENCODING=utf-8; python main.;y
- .NET MVC5简介(一)
就像是.NET Framework WebApi与.NET Core WebApi一样,.NET Framework MVC与.NET Core MVC的区别,也是框架的之间的区别.本系列先首先从.N ...