题意:判断两个子串是否相等
我们通过书可以知道:已知hash(s+t), hash(s), 那么就可以O(1)求出hash(t).
公式:hash(t) = hash(s+t)-hash(s)*p^t.length
所以处理完前缀hash值后,就可以通过上述方法来判断任意两个子串是否相等.
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long uLL;
const int P = 13331;
const int maxn = 1e6+5;
uLL value[maxn];
uLL plen[maxn];
char s[maxn];
void pre(){
int len = strlen(s+1);
for(int i = 1; i <= len; i++){
value[i] = value[i-1]*P + s[i]-'a'+1;
}
plen[0] = 1;
for(int i = 1; i <= len; i++){
plen[i] = plen[i-1]*P;
}
}
int main(){
scanf("%s", s+1);
pre(); //预处理
int m;
scanf("%d", &m);
while(m--){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if(value[b]-value[a-1]*plen[b-a+1] == value[d]-value[c-1]*plen[d-c+1]){ //直接判断两个子串的hash值是否相等
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}



京公网安备 11010502036488号