#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010,P=131;
typedef unsigned long long ULL;
int n,m;
char str[N];
ULL h[N],p[N];
ULL get(int l,int r)
{
// 求字符区间内的哈希值
return h[r]-h[l-1]*p[r-l+1];
}
int main(void)
{
scanf("%d%d",&n,&m);
scanf("%s",str+1);
p[0]=1;
for(int i=1;i<=n;i++)
{
h[i] = h[i-1]*P+str[i]-'0';
p[i] = p[i-1]*P;
}
while (m -- )
{
int l1,r1,l2,r2;
scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
if(get(l1,r1)==get(l2,r2)) puts("Yes");
else puts("No");
}
return 0;
}
实用算法模板——字符串哈希
最新推荐文章于 2024-10-16 00:00:43 发布