当前位置:网站首页>哈希表 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Introduction to CPU instruction set
- BOM DOM
- Embedded Software Engineer career planning
- 线性DP AcWing 902. 最短编辑距离
- Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
- mysql索引和事务
- VLAN experiment
- LeetCode—剑指 Offer 59 - I、59 - II
- CPU指令集介绍
猜你喜欢
深拷贝 事件总线
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
防抖 节流
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
BOM DOM
Jenkins user rights management
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
JSON序列化 与 解析
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
随机推荐
Openssh remote enumeration username vulnerability (cve-2018-15473)
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
LeetCode—<动态规划专项>剑指 Offer 19、49、60
"As a junior college student, I found out how difficult it is to counter attack after graduation."
Go学习笔记—多线程
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Tas (file d'attente prioritaire)
drools执行String规则或执行某个规则文件
[C language] Yang Hui triangle, customize the number of lines of the triangle
Go learning notes - multithreading
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
Brush questions --- binary tree --2
堆(優先級隊列)
线性DP AcWing 897. 最长公共子序列
趣味 面试题
CDA数据分析——AARRR增长模型的介绍、使用
CDA data analysis -- Introduction and use of aarrr growth model
Lombok common annotations
Anti shake throttle
Shuttle encapsulated AppBar