当前位置:网站首页>哈希表 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 1380. Lucky numbers in the matrix [two-dimensional array, matrix]
- ThreadLocal的简单理解
- drools中then部分的写法
- Drools dynamically add, modify, and delete rules
- 2.6 using recursion and stack - [tower of Hanoi problem]
- Addition, deletion, modification and query of MySQL table (Advanced)
- Leetcode209 subarray with the smallest length
- Fastdateformat why thread safe
- (C language) octal conversion decimal
- Sse/avx instruction set and API of SIMD
猜你喜欢

MySQL indexes and transactions

AI mid stage technology research

线性DP AcWing 902. 最短编辑距离

Use sqoop to export ads layer data to MySQL

Openssh remote enumeration username vulnerability (cve-2018-15473)

Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution

CDH6之Sqoop添加数据库驱动
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

甜心教主:王心凌

Sparkcontext: error initializing sparkcontext solution
随机推荐
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
VLAN experiment
AI mid stage technology research
Embedded Software Engineer career planning
Go学习笔记—多线程
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
Drools executes the specified rule
Writing method of then part in drools
甜心教主:王心凌
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Use sqoop to export ads layer data to MySQL
Go learning notes - multithreading
CDA数据分析——AARRR增长模型的介绍、使用
JSON序列化 与 解析
区间DP AcWing 282. 石子合并
Docker compose configuration mysql, redis, mongodb
Map和Set
包管理工具
堆(优先级队列)
染色法判定二分图 AcWing 860. 染色法判定二分图