当前位置:网站首页>Hash table acwing 841 String hash
Hash table acwing 841 String hash
2022-07-02 12:42:00 【T_ Y_ F666】
Hashtable AcWing 841. String hash
Original link
Algorithm tags
String hash Hash
Ideas

Simulation example 
Then the corresponding string hash
seek hash
Treat the corresponding string as a segment p Hexadecimal number 
Be careful : Characters cannot be mapped to 0
reason : If the A It maps to 0, Will cause all AA, AAA, …… Will all be 0.
Conflict handling
For data range < 2^64 data ,P Usually take 131 or 13331, warrantable hash Probability of conflict <0.01%
Code
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
// P Map the corresponding string hash value
// For data range < 2^64 data
//P Usually take 131 or 13331
// This ensures that hash Probability of conflict <0.01%
const int N = 100005, P = 131;
// h The storage string prefix corresponds to hash value p Store the decimal number
ull h[N], p[N];
char s[100005];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
// Corresponding string hash value
ull get(int l, int r){
return h[r]-h[l-1]*p[r-l+1];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read(), m=read();
scanf("%s", s+1);
// from 1 Start mapping
p[0]=1;
rep(i, 1, n+1){
p[i]=p[i-1]*P;
h[i]=h[i-1]*P+s[i];
}
while(m--){
int l1=read(), r1=read(), l2=read(), r2=read();
if(get(l1, r1)!=get(l2, r2)){
puts("No");
}else{
puts("Yes");
}
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- 模块化 CommonJS ES Module
- 线性DP AcWing 895. 最长上升子序列
- Day12 control flow if switch while do While guessing numbers game
- Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
- 防抖 节流
- Docsify deploy IIS
- Leetcode - Sword finger offer 59 - I, 59 - II
- VLAN experiment
- Redis sentinel mechanism and configuration
- Mui WebView down refresh pull-up load implementation
猜你喜欢

What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT

传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students

Enhance network security of kubernetes with cilium

Docker-compose配置Mysql,Redis,MongoDB

Adding database driver to sqoop of cdh6

C#修饰符

js1day(输入输出语法,数据类型,数据类型转换,var和let区别)

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

通过反射执行任意类的任意方法
随机推荐
Shutter encapsulated button
Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Leetcode - Sword finger offer 59 - I, 59 - II
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
Intel 内部指令 --- AVX和AVX2学习笔记
Simple understanding of ThreadLocal
Dijkstra AcWing 850. Dijkstra求最短路 II
8A 同步降压稳压器 TPS568230RJER_规格信息
JDBC 预防sql注入问题与解决方法[PreparedStatement]
[FFH] little bear driver calling process (take calling LED light driver as an example)
H5 to app
BOM DOM
Performance tuning project case
Use sqoop to export ads layer data to MySQL
OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
线性DP AcWing 896. 最长上升子序列 II
Input box assembly of the shutter package