0
点赞
收藏
分享

微信扫一扫

hash字符串哈希

https://www.acwing.com/problem/content/843/

求l->r的哈希值
h[r]-h[l-1]*P^(r-l+1)
注意要取模,可以用无符号的unsigned long long进行自然溢出

//https://www.acwing.com/problem/content/843/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int N=100005,P=131;
int n,m,l,r,ll,rr;
char s[N];
ULL p[N],h[N];
ULL get(int l,int r){
    return h[r]-h[l-1]*p[r-l+1];
}
int main(){
    cin>>n>>m;
    cin>>(s+1);
    p[0]=1;
    for(int i=1;i<=n;i++)p[i]=p[i-1]*P;
    for(int i=1;i<=n;i++)h[i]=h[i-1]*P+(s[i]-'a'+1);
    while(m--){
        cin>>l>>r>>ll>>rr;
        if(get(l,r)==get(ll,rr))cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

举报

相关推荐

0 条评论