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 ...
随机推荐
- oracle体系结构简介
oracle体系结构简介 一.物理存储结构 1.数据文件 存放数据库数据,以dbf为扩展名.将数据放在多个数据文件中, 再将数据文件分放在不同的硬盘中,可以提高存取速度. ...
- 10g+: Transportable Tablespaces Across Different Platforms (Doc ID 243304.1)
10g+: Transportable Tablespaces Across Different Platforms (Doc ID 243304.1) APPLIES TO: Oracle Data ...
- div里面的元素在【垂直 方向】上水平分布 使用calc()函数动态计算
1==>如何让div里面的元素在[垂直 方向]上水平分布.important-dec{ height: 121px; //必须固定高度 flex-direction: column; //垂直排 ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- C++(OI竞赛入门)学习指南一
#include<cstdio> 头文件#include包含 <头文件名称> #include<iostream> 需要头文件里面的功能 cst ...
- python奇闻杂技
第一天 01 从计算机到程序设计语言 02 python环境配置 03 实例一:温度转换 04 python语法分析 第二天 01 深入理解python语言 02 实例二,python蟒蛇配置 03 ...
- centos安装nginx并配置SSL证书
安装nginx的命令 sudo yum install epel-release sudo yum install nginx 让nginx随系统启动而启动 sudo systemctl enable ...
- vue--npm install只许成功,不许失败
npm安装时出现run `npm audit fix` to fix them, or `npm audit` for details 方案一: 1.如果你的项目里存在 package-lock.js ...
- Python连载42-异步协程函数
一. asyncio 1.python3.4开始引入标准库之中,内置对异步io的支持 2.asyncio本身是一个消息循环 3.步骤: (1)创建消息循环 (2)把协程导入 (3)关闭 4.举例: ...
- 接口测试用例yaml格式数据
1. login.yaml yaml文件 - name # 添加减号可以把用例转为list,每一部分是一个字典 url: /api/user/login method: post data: # 存放 ...