字符串哈希,区间哈希
参考字符串哈希模板题解析
题目

输入样例
aabbaabb
3
1 3 5 7
1 3 6 8
1 2 1 2
输出样例
Yes
No
Yes
代码
#include<iostream>
#include<cstring>
#include<cmath>
#define ull unsigned long long
using namespace std;
const ull P = 131;
ull h[1000003];
ull p[1000003];
char s[1000003];
int m,l1,r1,l2,r2;
int main(){
string temp;
cin>>temp;
p[0]=1;
h[0]=0;
for(int i=1;i<=temp.size();i++) {
s[i]=temp[i-1];
p[i]=p[i-1]*P;
h[i]=h[i-1]*P+s[i];
}
cin>>m;
for(int i=1;i<=m;i++){
cin>>l1>>r1>>l2>>r2;
ull t1= h[r1]-h[l1-1]*p[r1-l1+1];
ull t2=h[r2]-h[l2-1]*p[r2-l2+1];
if(t1==t2){
cout<<"Yes"<<endl;
}
else cout<<"No"<<endl;
}
return 0;
}
本文来自博客园,作者:InfiniteProgress,转载请注明原文链接:https://www.cnblogs.com/Serendip1ty/articles/18185083

浙公网安备 33010602011771号