思想
可以抽象为将字符串转换成P进制的数
算法1
C++ 代码
#include<iostream>
#include<string>
using namespace std;
typedef unsigned long long ULL;
const int N = 1e5 + 5,P = 131;
ULL h[N],p[N];
ULL query(int l,int r){
return h[r] - h[l - 1] * p[r - l + 1];
}
int main(){
int n,m;
cin >> n >> m;
string x;
cin >> x;
p[0] = 1;
h[0] = 0;
for(int i = 0; i < n; i ++){
p[i + 1] = p[i] * P;
h[i + 1] = h[i] * P + x[i];
}
while(m --){
int l1,r1,l2,r2;
cin >> l1 >> r1 >> l2 >> r2;
if(query(l1,r1) == query(l2,r2)) printf("Yes\n");
else printf("No\n");
}
return 0;
}