当前位置:网站首页>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
边栏推荐
- Enhance network security of kubernetes with cilium
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- . Net, C # basic knowledge
- China traffic sign detection data set
- Embedded Software Engineer career planning
- IPhone 6 plus is listed in Apple's "retro products" list
- Redis bloom filter
- kubenetes中port、targetPort、nodePort、containerPort的区别与联系
- BOM DOM
- 一些突然迸发出的程序思想(模块化处理)
猜你喜欢
Go learning notes - multithreading
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
In development, why do you find someone who is paid more than you but doesn't write any code?
arcgis js 4. Add pictures to x map
Simple use of drools decision table
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
浏览器存储方案
随机推荐
堆 AcWing 838. 堆排序
Deep Copy Event bus
[FFH] little bear driver calling process (take calling LED light driver as an example)
[ybtoj advanced training guidance] cross the river [BFS]
Drools executes string rules or executes a rule file
FBX import under ue4/ue5 runtime
分布式机器学习框架与高维实时推荐系统
应用LNK306GN-TL 转换器、非隔离电源
JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
Some sudden program ideas (modular processing)
哈希表 AcWing 841. 字符串哈希
Docker compose configuration mysql, redis, mongodb
Drools terminates the execution of other rules after executing one rule
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
Typora+docsify quick start
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Introduction to CPU instruction set
[I'm a mound pytorch tutorial] learning notes