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-判断回文
# 回文单词是从左到右和从右到左读相同的单词. # 例如:"detartrated"和"evitative"是回文 str_in = input('Input: ...
- Python机器学习笔记——One Class SVM
前言 最近老板有一个需求,做单样本检测,也就是说只有一个类别的数据集与标签,因为在工厂设备中,控制系统的任务是判断是是否有意外情况出现,例如产品质量过低,机器产生奇怪的震动或者机器零件脱落等.相对来说 ...
- python直接赋值、浅拷贝、深拷贝的区别
一:直接赋值 赋值,就是对象的引用,给对象起别名. i = 8j = iprint("值是:",i, "地址:",id(i))print("值是:&q ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- 2019-2020-1 20199305《Linux内核原理与分析》第四周作业
MenuOS的构造 一.Linux源代码的关键目录 block:存放块设备管理代码: crypto:存放常见加密算法的C语言代码: Documentation:存放一些文档: drivers:驱动目录 ...
- SQL Server 约束的增删改
1. 非空约束 列的为空性决定表中的行是否可以包含空值.空置(NULL)不同于零(0)/空白或者长度为零的字符串(“”). (1)创建非空约束 create table orders ( docent ...
- Linux 小工具
1. 截图工具 shutter 安装 sudo add-apt-repository ppa:shutter/ppa sudo apt-get update sudo apt-get install ...
- JavaScript:ES6的新特性
1.关键字 const:修饰常量.ES6之前只有变量的声明字段var,ES6开始引入常量关键字,被修饰的变量无法被修改. <script type="text/javascript&q ...
- npm查看本地包版本号和远程包的版本号
npm 查看远程包 第一种方法: npm info <packageName> 第二种方法: npm view <packageName> versions --json np ...
- app自动化测试环境搭建之node+appium+ADT+MUMU模拟器
一.安装Microsoft .NET Framework 4.5 检测本机已安装的程序中,是否已经安装Microsoft .NET Framework 4.5及以上的版本 如果没有安装,则获取安装文件 ...