当前位置:网站首页>哈希表 AcWing 841. 字符串哈希
哈希表 AcWing 841. 字符串哈希
2022-07-02 09:43:00 【T_Y_F666】
哈希表 AcWing 841. 字符串哈希
原题链接
算法标签
字符串哈希 哈希
思路

模拟举例
则对应字符串hash
求hash
把对应字符串看作一段p进制数
注意 :不能将字符映射为0
原因 :若将A映射为0, 则会导致所有AA, AAA, ……将都为0。
冲突处理
对于数据范围 < 2^64数据 ,P一般取131或13331,可保证hash冲突概率<0.01%
代码
#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为字符串映射对应hash值
//对于数据范围 < 2^64数据
//P一般取131或13331
//这样可保证hash冲突概率<0.01%
const int N = 100005, P = 131;
// h存储字符串前缀对应hash值 p存储该位进制数
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);
}
// 对应字符串hash值
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);
// 从1开始映射
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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Map和Set
- 堆(優先級隊列)
- Lekao: 22 year first-class fire engineer "technical practice" knowledge points
- Find the common ancestor of any two numbers in a binary tree
- Leetcode - Sword finger offer 37, 38
- 线性DP AcWing 902. 最短编辑距离
- [I'm a mound pytorch tutorial] learning notes
- Drools terminates the execution of other rules after executing one rule
- 浏览器node事件循环
- 计数类DP AcWing 900. 整数划分
猜你喜欢

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

mysql表的增删改查(进阶)

分布式机器学习框架与高维实时推荐系统

ThreadLocal的简单理解

使用Sqoop把ADS层数据导出到MySQL

Adding database driver to sqoop of cdh6

中国交通标志检测数据集

模块化 CommonJS ES Module

Addition, deletion, modification and query of MySQL table (Advanced)

There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
随机推荐
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Go learning notes - go based interprocess communication
CDA数据分析——Excel数据处理的常见知识点归纳
CPU指令集介绍
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
[I'm a mound pytorch tutorial] learning notes
Input box assembly of the shutter package
WSL 2 will not be installed yet? It's enough to read this article
IPhone 6 plus is listed in Apple's "retro products" list
Lombok common annotations
包管理工具
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
LeetCode—剑指 Offer 59 - I、59 - II
CDA数据分析——AARRR增长模型的介绍、使用
VLAN experiment
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
High performance erasure code coding
Performance tuning project case
高性能纠删码编码
Day12 control flow if switch while do While guessing numbers game