#include<bits/stdc++.h>
using namespace std;
// 字符串哈希 简单来说: 把字符串 映射 成数字
// 通过数字进行比较
typedef unsigned long long ULL;
const int N = 100010,P= 131;
// p[N] 记录 131 的 次方
// h[N] 表示 前缀哈希值和
ULL p[N],h[N];
char s[N];
// 预处理
void init(int n) {
p[0] = 1;h[0] =0;
for(int i = 1; i<= n; i++) {
p[i] = p[i-1]*P;
h[i] = h[i-1]*P + s[i];
}
}
// 找出某一区间的哈希值
ULL get(int l,int r) {
return h[r]-h[l-1]*p[r-l+1];
}
// 判断一下
bool substr(int l1, int r1, int l2 , int r2) {
return get(l1, r1) == get(l2, r2);
}
int main() {
int n,m;
cin>>n>>m;
// 输入字符串
scanf("%s", s + 1);
init(n);
int l1,l2,r1,r2;
while(m--) {
cin>>l1>>r1>>l2>>r2;
if(substr(l1,r1,l2,r2)) {
cout<<"Yes"<<endl;
} else {
cout<<"No"<<endl;
}
}
return 0;
}
字符串哈希(C++)
最新推荐文章于 2024-07-18 23:31:00 发布