Bunny Bunny (hash match

# Meaning of the questions
given a string, n th ask
each challenge consists of two sections, ask whether the two characters in the string section of equal

# Solution to a problem
that is determined from the beginning to the hash value of each subscript
each interrogation l, can be determined by subtraction when r
pretreatment array p each recording power p

 1 #include <bits/stdc++.h>
 2 #define ull unsigned long long
 3 using namespace std;
 4 const int N=1e6+10,P=131;
 5 char str[N];
 6 int m;
 7 ull h[N],p[N];
 8 int main(){
 9    scanf("%s",str+1);
10    cin>>m;
11    p[0]=1;
12    int n=strlen(str+1);
13    for(int i=1;i<=n;i++){
14        h[i]=h[i-1]*P+(str[i]-'a'+1);
15        p[i]=p[i-1]*P;
16    }
17    while(m--){
18       int l1,r1,l2,r2;
19       cin>>l1>>r1>>l2>>r2;
20       if(h[r1] - (h[l1-1] * p[r1-l1+1]) == h[r2] - (h[l2-1] * p[r2-l2+1]))
21          cout<<"Yes"<<endl;
22       else
23          cout<<"No"<<endl;
24    }
25 }

 

Guess you like

Origin www.cnblogs.com/hhyx/p/12514854.html