Advanced Guide to Algorithm Competition---0x14 (Hash) Bunny and Bunny

Topic

Insert picture description here

answer

  1. It is a string hash template question, preprocess the string to get the hash value, and then ask each time whether the hash value of the interval is equal.
  2. Click here to explain the string hash

Code

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
typedef unsigned long long ULL;
const int N = 1e6 + 10;
const int P = 131;

char str[N];
ULL h[N], p[N];

ULL get(int l, int r) {
    
    
    return h[r] - h[l-1] * p[r - l + 1];
}

int main() {
    
    

    scanf("%s", str + 1);
    int n = strlen(str + 1);
    p[0] = 1;
    for (int i = 1; i <= n; i++) {
    
    
        h[i] = h[i - 1] * P + str[i];
        p[i] = p[i - 1] * P;
    }
    int t;
    cin >> t;
    while (t--) {
    
    
        int l1, r1, l2, r2;
        cin >> l1 >> r1 >> l2 >> r2;
        int v1 = get(l1, r1);
        int v2 = get(l2, r2);
        if (v1 == v2) cout << "Yes" << endl;
        else cout << "No" << endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/113839107