哈希
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef unsigned long long ull;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
char s[N];
int q;
int l1,r1,l2,r2;
ull p[N],Hash[N];
ull get_hash(int l,int r) {
return Hash[r] - Hash[l - 1] * p[r - l + 1];
}
int main() {
scanf("%s",s + 1);
scanf("%d",&q);
int n = strlen(s+1);
p[0] = 1;
for(int i = 1;i<=n;i++) {
p[i] = p[i - 1] * 131;
Hash[i] = Hash[i-1] *131 + s[i] - 'a' + 1;
}
for(int i = 1;i<=q;i++) {
scanf("%d %d %d %d",&l1,&r1,&l2,&r2);
if(get_hash(l1,r1) == get_hash(l2,r2)) {
printf("Yes\n");
}else {
printf("No\n");
}
}
return 0;
}
我们直接把hash函数封装起来,方便后面使用