当前位置:网站首页>补坑 模拟散列表
补坑 模拟散列表
2022-08-08 05:42:00 【一条小小yu】
题目:

map也能做,主要学一下hash表的实现方式,之前3、4月份的时候疯狂补坑备考,但是选择了洛谷和b站,现在看来还是acwing比较好,因为有视频。
拉链法代码
拉链法就是1-N,然后每个i一个链表,感觉挺好理解,上代码。
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e5 + 3; // 取大于1e5的第一个质数,取质数冲突的概率最小 可以百度
//* 开一个槽 h
int h[N], e[N], ne[N], idx; //邻接表
void insert(int x) {
// c++中如果是负数 那他取模也是负的 所以 加N 再 %N 就一定是一个正数
int k = (x % N + N) % N;
e[idx] = x;
ne[idx] = h[k];
h[k] = idx++;
}
bool find(int x) {
//用上面同样的 Hash函数 讲x映射到 从 0-1e5 之间的数
int k = (x % N + N) % N;
for (int i = h[k]; i != -1; i = ne[i]) {
if (e[i] == x) {
return true;
}
}
return false;
}
int n;
int main() {
cin >> n;
memset(h, -1, sizeof h); //将槽先清空 空指针一般用 -1 来表示
while (n--) {
string op;
int x;
cin >> op >> x;
if (op == "I") {
insert(x);
} else {
if (find(x)) {
puts("Yes");
} else {
puts("No");
}
}
}
return 0;
}
开放寻址法:
#include <cstring>
#include <iostream>
using namespace std;
//开放寻址法一般开 数据范围的 2~3倍, 这样大概率就没有冲突了
const int N = 2e5 + 3; //大于数据范围的第一个质数
const int null = 0x3f3f3f3f; //规定空指针为 null 0x3f3f3f3f
int h[N];
int find(int x) {
int t = (x % N + N) % N;
while (h[t] != null && h[t] != x) {
t++;
if (t == N) {
t = 0;
}
}
return t; //如果这个位置是空的, 则返回的是他应该存储的位置
}
int n;
int main() {
cin >> n;
memset(h, 0x3f, sizeof h); //规定空指针为 0x3f3f3f3f
while (n--) {
string op;
int x;
cin >> op >> x;
if (op == "I") {
h[find(x)] = x;
} else {
if (h[find(x)] == null) {
puts("No");
} else {
puts("Yes");
}
}
}
return 0;
}
边栏推荐
- 基础了解虚拟 DOM
- Week 9 10 Neural Networks
- 分布式事务 :可靠消息最终一致性方案
- 数字IC设计中为什么要避免锁存器(Latches)
- TSF Microservice Governance Combat Series (2) - Service Routing
- 基本工具-NETCAT(telnet-banner、传输文本信息)
- 查询时间内用户分布的sql语句
- The big and small end problem caused by union union
- apifox使用文档之环境变量 / 全局变量 / 临时变量附apifox学习路线图
- 数字IC设计笔试题汇总(四):一些基础知识点
猜你喜欢

TCP/IP基本实现

Unity-CharacterController (Character Controller)

TSF微服务治理实战系列(二)——服务路由

Distributed Transactions: A Reliable Message Eventual Consistency Scheme

查询跟踪多家快递单号,筛选某一时间发货的单号

KDD‘22推荐系统论文梳理(24篇研究&36篇应用论文)

数字IC设计笔试题汇总(四):一些基础知识点

sqlmap+dnslog注入复现

Query and track multiple express tracking numbers, and filter the tracking numbers shipped at a certain time

浅学软件逆向笔记(1)
随机推荐
使用 Zap 和 W3af 进行 Web 应用程序漏洞评估
邮件钓鱼上线cobalstrike
分页组件的使用
文件操作 - IO
TCP/IP基本实现
Database sub-database sub-table, when?How to divide?
Filter 过滤器的使用
121道分布式面试题和答案
Hundreds of billions, large-scale: performance tuning practice of Tencent's super-large Apache Pulsar cluster
阿里云的数据库怎么提高访问速度的本地的打开的方式是www.zgysffm.com怎样的?
说说Redis分布式锁的原理和实现蚂【蚁金服三面】
Efficient and beautiful scrolling component Slivers of Flutter tutorial (tutorial includes source code)
Week 8 Generative Adversarial Networks
Unity mouse cursor usage learning
Redis In Action —— Advanced —— 数据主从同步原理 —— 全量同步 与 增量同步 工作流程及原理 —— 以及如何利用 docker 容器技术快速模拟单机 Redis 集群
"Public Administration" exam key points and answers
Go-Excelize API source code reading (10) - SetActiveSheet(index int)
Servlet---ServletConfig类使用介绍
Typescript namespace
postman---postman parameterization