字符串哈希,区间哈希

参考字符串哈希模板题解析

题目

输入样例

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;
}
posted @ 2024-05-10 18:21  InfiniteProgress  阅读(10)  评论(0)    收藏  举报