当前位置:网站首页>Pit Filling Simulated Hash Table
Pit Filling Simulated Hash Table
2022-08-08 05:45:00 【a little yu】
Title:

The map can also be done. The main thing is to learn how to implement the hash table. In the past March and April, I was crazy to fill the pits to prepare for the test, but I chose Luogu and B station. Now it seems that acwing is better because there are videos.
Zip code
The zipper method is 1-N, and then there is a linked list for each i. It feels very easy to understand. Let's go to the code.
#include #include using namespace std;const int N = 1e5 + 3; // Take the first prime number greater than 1e5, the probability of taking the prime number conflict is the smallest, you can Baidu//* open a slot hint h[N], e[N], ne[N], idx; //adjacency listvoid insert(int x) {// If it is a negative number in c++, its modulo is also negative, so adding N and then %N must be a positive numberint k = (x % N + N) % N;e[idx] = x;ne[idx] = h[k];h[k] = idx++;}bool find(int x) {//Use the same Hash function above to map x to a number between 0-1e5int 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); //Empty the slot first. The null pointer is generally represented by -1while (n--) {string op;int x;cin >> op >> x;if (op == "I") {insert(x);} else {if (find(x)) {puts("Yes");} else {puts("No");}}}return 0;} Open Addressing:
#include #include using namespace std;//The open addressing method generally opens 2~3 times the data range, so there is a high probability that there will be no conflictconst int N = 2e5 + 3; //The first prime number greater than the data rangeconst int null = 0x3f3f3f3f; //Specify the null pointer as null 0x3f3f3f3fint 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; //If this location is empty, it returns the location where it should be stored}int n;int main() {cin >> n;memset(h, 0x3f, sizeof h); //Specify the null pointer as 0x3f3f3f3fwhile (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;} 边栏推荐
猜你喜欢

Unity mouse cursor usage learning

Redis set to start automatically at boot

Checkerboard Coloring Problem

如何批量导入文件,并全部自定义重命名为相同文件名

仿QQ好友列表,QListWidget!

14.Unity2D 横版 粒子系统特效 飙血粒子+高处落地粒子+对象池管理所有粒子

说说Redis分布式锁的原理和实现蚂【蚁金服三面】

std::thread创建线程,使用std::ref()传递类对象参数

毕设——基于人脸表情的桌面交互精灵设计(分享一下成果,附上人脸表情的数据集和自己训练出来yolov5模型以及基于PYQT5运行yolov5的交互界面)

76. The minimum cover substring
随机推荐
Efficient and beautiful scrolling component Slivers of Flutter tutorial (tutorial includes source code)
Day7:面试必考选择题
验证的计划
查询时间内用户分布的sql语句
【RPC】Mercury RPC
sqlmap+dnslog注入复现
Week 8 Transformer Language Models and Implications
【MySQL】——事务的基本概念
云计算和云服务,云计算
并查集按秩合并rank数组
std::thread创建线程,使用std::ref()传递类对象参数
预处理笔记
《公共管理学》考试重点及答案
整数分块例题
APISIX Ingress v1.5-rc1 发布
Shorthand for flex layout properties
CAP定理实例分析
Postman显示验证码图片(base64字符串)
Go-Excelize API源码阅读(十)—— SetActiveSheet(index int)
Filter 过滤器的使用