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 ...
随机推荐
- Python安装常见问题(1):zipimport.ZipImportError: can't decompress data(此问题不解决pip安装不成功)
在CentOS以及其他的Linux系统中遇到安装包安装错误的原因,大多数都是因为缺少依赖包导致的,所以对于错误:zipimport.ZipImportError: can’t decompress d ...
- mysql中用命令行复制表结构(数据)
mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2; 或 CREATE TABLE 新表 ...
- Ubuntu下Xilinx Linux内核编译问题,出现“缺少ncurses”libraries
对官方提供的内核源码包进行解压缩,进入到内核目录,使用make menuconfig后,发现提示以下错误: *** Unable to find the ncurses libraries or th ...
- Ubuntu 18.04 安装 python 的 redis 库
安装 如果只是安装了 python2.x 或者 python3.x,直接安装即可,命令如下: pip install redis 如果是同时安装了 python2.x 和 python3.x 的,则需 ...
- 注意设置httpclient连接数
在使用Httpclient的过程中,当访问量增大的时候,会发现本地的连接等待时间急剧增加,例如从400ms增加到 78000ms,之前一直以为是航信系统问题,后面经过检查才发现,原来是本地httpcl ...
- Java程序猿怎么才能月薪过万?
每一个略微有点长进的人,都应该把作业里的前三名作为自己斗争的政策和对手.你离成为冠军Java程序员还有多远,看完这篇你就知道了. 软件工程师的作业生涯里,知识有一个三年的半衰期.这意味着三年后,你所具 ...
- C语言中,字符型数字与常数型数字的加减实现
char in-str[10],out-str[10]; for(int i=0;i<10;i++) { out-str[i]=9-(in-str[i]-'0')+'0'; }
- windows7 php 环境架设
参考 https://www.jb51.net/article/38048.htm 常见问题解决方案 https://blog.csdn.net/w_yunlong/article/det ...
- typescript里一些有趣的点
联合类型 在原生的JS里,null和undefined经常会导致BUG的产生, 在ts里,你又想用null,又担心出错的时候 你可以考虑用联合类型,当某值可能为 number或null,你可以声明它的 ...
- Python连载53-UDP、TCP、FTP编程实例
一.服务器程序要求永远运行,一般用死循环来处理 1.服务器改造版本V03(主程序 原封不动,这里只修改了运行的程序) if __name__ == "__main__": whil ...