当前位置:网站首页>哈希表 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- BOM DOM
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- Introduction to CPU instruction set
- Leetcode14 longest public prefix
- Multiply LCA (nearest common ancestor)
- (C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
- 线性DP AcWing 902. 最短编辑距离
- Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
- Fluent fluent library encapsulation
- 中国交通标志检测数据集
猜你喜欢
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Map和Set
Enhance network security of kubernetes with cilium
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
CDH6之Sqoop添加数据库驱动
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
线性DP AcWing 895. 最长上升子序列
Openssh remote enumeration username vulnerability (cve-2018-15473)
Initial JDBC programming
随机推荐
[C language] convert decimal numbers to binary numbers
kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
Sort---
Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
包管理工具
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
怎样写一篇赏心悦目的英文数学论文
Drools executes the specified rule
刷题---二叉树--2
Distributed machine learning framework and high-dimensional real-time recommendation system
BOM DOM
drools执行完某个规则后终止别的规则执行
Deep copy event bus
arcgis js 4. Add pictures to x map
arcgis js 4.x 地图中加入图片
Find the common ancestor of any two numbers in a binary tree
IPhone 6 plus is listed in Apple's "retro products" list
How to write a pleasing English mathematical paper
Deep Copy Event bus
CDH6之Sqoop添加数据库驱动