当前位置:网站首页>[25. Hash table]
[25. Hash table]
2022-07-25 01:03:00 【Little silly bird_ coding】
Simulation hash table
summary
Hashtablealso calledHash table, Generally byHash function ( Hash function )AndLinked listStructure together .
purpose
- Additive elements
- Find the corresponding element through history .( Hash is used because its time complexity is close to O(1))
Ideas
- Compare a
Complex data structuresMap to subscript from0~NWithin the easy to maintain value range . Because the value range is relatively simple 、 Small scope 、 It will produceDifferent raw value information is Hash Function maps to the same value, So deal with this conflict . - There are two ways to deal with conflicts :
Zipper methodandOpen addressing ( Squatting method )
The difference between hash table and discretization
- Previously, discretization is also through the method of mapping , Map the value of a larger number to a smaller number .
- Discretization is a special hash method , Here is the hash method in the general sense .
- An important feature of discretization is the need
Keep the order.Hash This function needsMonotone increasing.

Zipper method
step
H(x) = X mod P. there P Is a relatively large number , But it cannot exceed the specified N- Handling conflicts
The main way to deal with conflicts is through arrays , Next, pull a linked list , So it's called zipper method for short
When several original values , adopt Hash When a function maps to the same value , We will mount them to the linked list under the value in turn .
To reduce the number of conflicts , here mod The number of is generally a prime number
Find greater than 100000 The prime number of
for (int i = 100000;; i ++)
{
bool flag = true;
for ( int j = 2; j < i; j ++)
{
if (i %j == 0)
{
flag = false;
break;
}
}
if (flag)
{
cout << i << endl;
break;
}
}
The result of the operation is 100003
Code
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100003;
int h[N], e[N], ne[N]; //h[N] It means groove ,e[] and ne[] Represents a linked list , We need to put the linked list on the slot .
int idx;
void insert(int x)
{
int k = (x % N + N) % N; // The hash function will x Map to from 0~N A number between , Prevent negative numbers, so +N,%N;
//c++ If it's negative Then his modulus is also negative therefore Add N Again %N It must be a positive number
e[idx] = x; // This is the single chain list , Number of inserts
ne[idx] = h[k];
h[k] = idx;
idx ++;
}
bool find(int x)
{
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 main()
{
int n ;
scanf("%d", &n);
memset(h, -1, sizeof(h)); // Empty the tank , For empty finger needle -1 Express
while (n --)
{
char op[2];
int x;
scanf("%s%d", op, &x);
if (op[0] == 'I') insert(x);
else
{
if (find(x)) puts("Yes");
else puts("No");
}
}
return 0;
}
Open addressing
- Open addressing is generally open
Data range 2~3 times, So there is no conflict in the probability - Zipper method N Is the closest to the maximum range (10 Of 5 Prime number to the power ) In open addressing , It usually needs to be opened
Two to three times the fixed slot( To prevent conflict , So choose 2*10 The nearest prime number to the fifth power of
( Only in this way , The probability of no conflict is 99.99)
step 
Code
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200003; // When opening the pit , It's usually 2~3 times , At this time, it will not cause overflow
null = 0x3f3f3f3f; // Agree on a sign , If a number in the array is equal to this number , It means that there is no one in this position .( Use a number to indicate that this position is empty )
int h[N]; // Open addressing , Just need an array . Squatting method
int find(int x) // If x Does it already exist in the hash table , Just return x Where it is , If x Does not exist in the hash table , It returns the location where it should be stored
{
int k =(x % N + N) % N; // The hash function will x Map to from 0~N A number between , Prevent negative numbers, so +N,%N;
while (h[k] != null && h[k] != x)
{
k ++;
if (k == N) k = 0; // If not , Just look from the beginning
}
return k;
}
int main()
{
int n ;
scanf("%d", &n);
memset(h, 0x3f, sizeof(h)); // By byte memset( Not by number ),h It's a int Type of the array , Altogether 4 Bytes , Every byte is 0x3f
// So each number is 4 individual 0x3f,int Yes 4 Bytes , Turn every byte into 3f
while (n --)
{
char op[2];
int x;
scanf("%s%d", op, &x);
if (op[0] == 'I')
{
int k = find(x);
h[k] = x;
}
else
{
int k = find(x);
if (h[k] != null) puts("Yes");
else puts("No");
}
}
return 0;
}
// cout << 0x3f << endl; 63
// cout << 0x3f3f3f3f <<endl; 1061109567
边栏推荐
- Esp32 OLED lvgl displays common Chinese characters
- Financial RPA robot enables enterprises to open a new era of intelligence
- mysql初次安装的root密码是什么
- The use of Multimeter in circuit analysis experiment of Shandong University
- Introduction to thread pool
- Join MotoGP Monster Energy British Grand Prix!
- 第四章 驱动子系统开发
- How to implement a state machine?
- JS convert pseudo array to array
- Related knowledge of paging
猜你喜欢

Unity slider slider development

Game partner topic: the cooperation between breederdao and monkeyleague kicked off

Summary of MATLAB basic grammar

The font changes with the change of the form

Listing of China graphite: the market value is nearly HK $1.2 billion, achieving a zero breakthrough in the listing of Hegang private enterprises
![Nacos hand to hand teaching [i] dynamic configuration of Nacos](/img/c4/ae29475c795e879683227de5ba3cfc.png)
Nacos hand to hand teaching [i] dynamic configuration of Nacos

Detailed explanation of alexnet of paddlepaddle paper series (with source code)

Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos

C # "learning code snippet" - recursively obtain all files under the folder

BGP机房和BGP
随机推荐
Research and Multisim Simulation of linear circuit characteristics (engineering documents attached)
Add the two numbers in the linked list of the second question of C language. Ergodic method
Record the bugs encountered and some work experience
Educational events
Current events on July 20
BGP机房和BGP
record
7.15 - daily question - 408
Construction of Seata multilingual system
The position of the nth occurrence of MySQL in the string
Automated test series selenium three kinds of waiting for detailed explanation
The first meta universe auction of Chen Danqing's printmaking works will open tomorrow!
Breederdao's first proposal was released: the constitution of the Dao organization
UXDB在不知道明文密码的情况下重置密码
Number of palindromes in question 5 of C language deduction (two methods)
Cloud native observability tracking technology in the eyes of Baidu engineers
Login and payment arrangement in uniapp
基于ABP实现DDD--领域逻辑和应用逻辑
[icore4 dual core core _arm] routine 22: LwIP_ UDP experiment Ethernet data transmission
The current situation of the industry is disappointing. After working, I returned to UC Berkeley to study for a doctoral degree