当前位置:网站首页>The concept and application of hash table
The concept and application of hash table
2022-07-04 20:55:00 【_ Liu Xiaoyu】
List of articles
Hash table is an algorithm that we have to master
1. hash Tabular 2 Applications
It is commonly used to convert a large range of data , But it is usually discrete , Map a large range of data to a small range of question types . Explained between Interval and This is the type of question .
1.1. Zipper method
such as L The value range is [-109 - 109] , We need to map to 105 Within the scope of
The common practice is :
- First pair x mod 105 ( there 105 It is random , If you want less conflict value , The experience value is 131 Or is it 13331)
- Handling conflicts ( Because mapping from a large range to a small range may probably repeat ) Example : h(11) = 3, h(23) = 3 ( here h A function is a hash function )
Generally in the hash table , As long as the operation is to add and find , If you really need to delete , A mark should be made on the corresponding mark position
Look for a greater than 100000 The prime number of
code:
#include <iostream>
using namespace std;
int main()
{
for(int i = 100000;; i ++)
{
bool flag = true;
for(int j = 2; j * j <= i; j ++)
if(i % j == 0)
{
flag = false;
break;
}
if (flag)
{
cout << i << endl;
break;
}
}
return 0;
}
1.2. Open addressing
Another open addressing method is introduced in this article :
https://blog.csdn.net/qq_39486027/article/details/125096272
Open addressing : Generally, the open array is originally a small range 2-3 times
add to :
- after h(x) Find out where it should exist ;
- See if there is anyone in this position , If someone goes on , Until no one , preservation ; If no one , Save directly .
lookup :
- Directly find the specified location , If there are elements , Compare whether to find the value , It's not equal to , See if the next position is , By analogy , Until you find or no element next time
Delete :
- To find the first , If you find it , Make a mark
Here is a related simulation hash Get examples :
2. hash Examples of tables
subject :
Maintain a collection , The following operations are supported :
I x, Insert a number x;
Q x, Ask the number x Whether there has been... In the collection ;
Now it's time to N operations , For each query operation, the corresponding result is output .
Input format
The first line contains integers N, Represents the number of operations .
Next N That's ok , Each line contains an operation instruction , The operation instruction is I x,Q x One of the .
Output format
For each inquiry instruction Q x, Output a query result , If x In the collection , The output Yes, Otherwise output No.
Each result takes up one line .
Data range
1≤N≤105
−109≤x≤109
sample input :
5
I 1
I 2
I 3
Q 2
Q 5
sample output :
Yes
No
2.1 Zipper method code implementation :
Code:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100003;
int h[N], e[N], ne[N], idx;
/// Zipper method
void insert(int x)
{
int k = (x % N + N) % N;
e[idx] = x; // Insertion of single chain table
ne[idx] = h[k];
h[k] = 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;
cin >> n;
memset(h, -1, sizeof h);
while(n --)
{
char op[2];
int x;
cin >> op >> x;
if(op[0] =='I') insert(x);
else
{
if(find(x)) puts("Yes");
else puts("No");
}
}
return 0;
}
2.2 Open addressing code implementation :
Code:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200003, null = 0x3f3f3f3f;
int h[N];
// Open addressing
// Back to x : exist , return x The location of
// If it doesn't exist , What is returned is the location that should be stored
int find(int x)
{
int k = (x % N + N) % N;
while(h[k] != null && h[k] != x)
{
k++;
if(k == N) k = 0;
}
return k;
}
int main()
{
int n;
cin >> n;
memset(h, 0x3f, sizeof h);
while(n --)
{
char op[2];
int x;
cin >> 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;
}
3. character string hash The way
The other is string hash.
3.1 character string hash Treatment principle
Here is about string prefix hash
for example : str = “ABCDEFG”
We make use of hash The function needs to find the string prefix hash , What is a string prefix hash
h[0] = 0
h[1] = “A” Of hash value
h[2] = “AB” Of hash value
h[3] = “ABC” Of hash value
…
So how do we define this prefix hash value , The step method used here is like this
for example : str = “ABCD”
First step : Put the above characters Mapped from 1 At the beginning A - 1; B - 2 …
The second step : Consider the above string as a P Binary number (1234)p . there P It's an experience value : 131 Or is it 13331.
The third step : And then put the top P The base number is converted to Decimal numbers . then mod A number 264. To ensure that there is no conflict .
(1 * P3 + 2 * P2 + 3 * P 1 + 4 * P 0) mod (264).
Here it maps to hash Values are guaranteed not to conflict , Therefore, there is no need to consider the way of conflict handling .
【 Be careful 】:
- Characters cannot be mapped to 0
2. Suppose the character is good enough , There is no conflict , Generally speaking , p take 131、13331 yes ;q take 264 when , There is little conflict .
3.2 String hash Calculation derivation
With the prefix string calculated above hash value , We can calculate the hash value , How to do it?
for instance : Requirements for joining L-R In this section hash value .
Because what is known is the prefix hash value ,h[R] 、h[L - 1] We all know .
We regard this paragraph as P Binary number , So the left side is high , The right is low .
The calculation formula is given in the figure
In some topics , It can only be processed with string hash , use kmp It's not as good as this method , So he must know
3.3 String hash application example
Here is a related example
subject :
Given a length of n String , Re given m A asked , Each query contains four integers l1,r1,l2,r2, Please judge [l1,r1] and [l2,r2] Whether the strings and substrings contained in these two intervals are exactly the same .
The string contains only uppercase and lowercase English letters and numbers .
Input format
The first line contains integers n and m, Represents the length of the string and the number of queries .
The second line contains a length of n String , The string contains only uppercase and lowercase English letters and numbers .
Next m That's ok , Each line contains four integers l1,r1,l2,r2, Indicates the two intervals involved in a query .
Be careful , The position of the string is from 1 Numbered starting .
Output format
Output a result for each query , If two string substrings are identical, output “Yes”, Otherwise output “No”.
Each result takes up one line .
Data range
1≤n,m≤105
sample input :
8 3
aabbaabb
1 3 5 7
1 3 6 8
1 2 1 2
sample output :
Yes
No
Yes
3.4 String hash code implementation
Code:
#include <iostream>
using namespace std;
typedef unsigned long long ULL;
const int N = 100010, P = 131; // there P It's experience 131 , perhaps 13331
int n,m;
char str[N];
// Here we use unsigned long long Storage is equivalent to mod 2 ^ 64, Because it will overflow if it exceeds
ULL h[N], p[N]; // h[] Is to store the string hash value p[] Is stored p Subordinate
ULL get(int l, int r)
{
return h[r] - h[l -1] * p[r - l + 1]; // Section hash Formula
}
int main()
{
scanf("%d%d%s", &n, &m, str + 1);
p[0] = 1;
for(int i=1; i<=n; i++)
{
p[i] = p[i-1] * P; // p Array save The power of calculation
h[i] = h[i-1] * P +str[i]; // Calculate the prefix of the string , The back is 0 Time So just add str[i] That's it
}
while(m -- )
{
int l1, r1, l2, r2;
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
if(get(l1, r1) == get(l2, r2)) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
Recommend a free open course of zero sound College , Personally, I think the teacher spoke well , Share with you :Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now
边栏推荐
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- LeetCode 871. Minimum refueling times
- 字节测试工程师十年经验直击UI 自动化测试痛点
- NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- 分析伦敦银走势图的技巧
- 精选综述 | 用于白内障分级/分类的机器学习技术
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- 针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
- 黄金k线图中的三角形有几种?
猜你喜欢
How does win11 search for wireless displays? Win11 method of finding wireless display device
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
Understand Alibaba cloud's secret weapon "dragon architecture" in the article "science popularization talent"
Flet tutorial 04 basic introduction to filledtonalbutton (tutorial includes source code)
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
电脑页面不能全屏怎么办?Win11页面不能全屏的解决方法
工厂从自动化到数字孪生,图扑能干什么?
Selected review | machine learning technology for Cataract Classification / classification
随机推荐
托管式服务网络:云原生时代的应用体系架构进化
剑指 Offer II 80-100(持续更新)
精选综述 | 用于白内障分级/分类的机器学习技术
九齐单片机NY8B062D单按键控制4种LED状态
记一次重复造轮子(Obsidian 插件设置说明汉化)
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
go语言笔记(4)go常用管理命令
Implementation of redis distributed lock
What if win11u disk refuses access? An effective solution to win11u disk access denial
tcp为啥是三次握手和四次挥手
Play the music of youth
左右最值最大差问题
Taishan Office Technology Lecture: about the order of background (shading and highlighting)
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
jekins初始化密码没有或找不到
易周金融 | Q1保险行业活跃人数8688.67万人 19家支付机构牌照被注销
QT writing the Internet of things management platform 38- multiple database support
Summary of the mistakes in the use of qpainter in QT gobang man-machine game
二叉树的四种遍历方式以及中序后序、前序中序、前序后序、层序创建二叉树【专为力扣刷题而打造】
CDGA|数据治理不得不坚持的六个原则