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中进入容器命令行及后台运行
Docker中我们一般会有两种执行命令的方式,一种是直接进入容器的命令行,在终端执行并查看结果,一种是在后台执行,并不会在终端查看结果. 1.进入容器命令行 su root docker run -i ...
- Shell—各种括号的用法
括号的种类 小括号,圆括号 ( ) 中括号,方括号 [ ] 大括号.花括号 { } 一.单小括号 () 1.另开命令组——小括号中的命令将会新开启一个子shell独立顺序运行,所以括号中的变量不能够被 ...
- CCSpriteFrameCache的使用
配置环境:win7+Cocos2d-x.2.0.3+VS2012 CCSpriteFrameCache是帧缓存类. 通过plist文件导入图片 CCSpriteFrameCache::sharedSp ...
- 浅谈vue中的计算属性和侦听属性
计算属性 计算属性用于处理复杂的业务逻辑 计算属性具有依赖性,计算属性依赖 data中的初始值,只有当初始值改变的时候,计算属性才会再次计算 计算属性一般书写为一个函数,返回了一个值,这个值具有依赖性 ...
- Linux学习笔记-第19天 结束了。突然感觉配置一个服务好简单的样子
课程结束了,这本书又过了一遍,感觉学习到了不少的新知识.虽然整个过程老师讲的有点仓促,但回头想想身处于这个知识大爆炸的时代,学习不单要追求知识面宽广,更需要注重学习的效率,某种角度来讲,这也是一种鞭策 ...
- SQL查询--索引
索引概念和作用 索引是建立在表上的可选对象,目的是为了提高查询速度. 如果要在表中查询指定的记录,在没有索引的情况下,必须遍历整个表,而有了索引之后,只需要在索引中找到符合查询条件的索引字段值,就可以 ...
- React中添加注释
React中的注释,其实确切来讲是jsx中的注释: {/*单行注释*/} {/* 多 行 注 释 */}
- EEPROM的操作---SPI接口和I2C接口
参考:http://blog.csdn.net/yuanlulu/article/details/6163106 ROM最初不能编程,出厂什么内容就永远什么内容,不灵活.后来出现了PROM,可以自己写 ...
- 划词标注1——使用svg绘制换行文本并自动识别库中字典数据
业务需求 给出一段文本,自动识别出文本中包含的关键字信息,关键字是库里已知的数据,根据类型的不同显示出不同的颜色 业务分析 1)采用css:文本识别出来后,根据识别出的文本更改对应文本的dom,通过更 ...
- struts2文件上传报错
说明上传的文件为空,检查上传文件名