AcWing 841. 字符串哈希
原题链接
简单
作者:
dsyami
,
2021-06-03 20:20:35
,
所有人可见
,
阅读 251
#include <iostream>
using namespace std;
typedef unsigned long long ULL; //ULL相当于模2^64哈希
const int N = 1e5 + 10, P = 131; //131, 13331
int n, m;
char str[N];
ULL p[N], h[N]; //h为哈希值数组,p为每一位的权值
ULL query(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
cin >> n >> m >> str + 1;
p[0] = 1;
for (int i = 1; str[i]; i ++ )
{
p[i] = p[i - 1] * P;
h[i] = h[i - 1] * P + str[i];
}
while (m -- )
{
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
if (query(l1, r1) == query(l2, r2)) puts("Yes");
else puts("No");
}
return 0;
}