AcWing 841. 字符串哈希
原题链接
简单
作者:
Value
,
2021-03-26 15:27:47
,
所有人可见
,
阅读 264
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1E5 + 10, P = 131;
typedef unsigned long long ull;
ull h[N], p[N];
char str[N];
ull get_hash(int l, int r){
return h[r] - h[l - 1] * p[r - l + 1];
}
int main(){
int n, q; scanf("%d%d%s", &n, &q, str + 1);
p[0] = 1;
for(int i = 1; i <= n; i ++ ){
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + str[i];
}
while(q -- ){
int l1, r1, l2, r2; scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
cout << (get_hash(l1, r1) == get_hash(l2, r2) ? "Yes" : "No") << endl;
}
return 0;
}