87062024-01-25 20:42:25szilTúlcsorduláscpp17Runtime error 40/10032ms6280 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int MAXN = 10'010;
const int HASH_CNT = 2;
const ll MOD = 1e9+7;

ll mult(ll a, ll b){return (a*b)%MOD;}
ll poww(ll a, ll b){
    if(b==0)return 1LL;
    if(b&1)return mult(poww(a, b-1), a);
    ll c = poww(a, b/2);
    return mult(c, c);
}

ll eredeti_primes[2] = {3, 5};
ll primes[MAXN][HASH_CNT], inv_primes[MAXN][HASH_CNT];


struct Hash {
    ll val[HASH_CNT];

    bool operator==(const Hash &x) {
        for (int i = 0; i < HASH_CNT; i++) if (val[i] != x.val[i]) return false;
        return true;
    }
};

bool a[MAXN], b[MAXN];
Hash ha[MAXN], hb[MAXN];

Hash H(Hash *arr, int l, int r) {
    Hash res;
    for (int i = 0; i < HASH_CNT; i++) {
        res.val[i] = (arr[r].val[i] - arr[l-1].val[i] + MOD) % MOD;
        res.val[i] = mult(res.val[i], inv_primes[l-1][i]); // TODO check
    }
    return res;
}

int main(){
    cin.tie(0); cin.sync_with_stdio(0);
    for(int i = 0; i < HASH_CNT; i++){
        primes[0][i] = 1;
        inv_primes[0][i] = 1;
        for(int j = 1; j < MAXN; j++){
            primes[j][i] = (primes[j-1][i] * eredeti_primes[i]) % MOD;
            inv_primes[j][i] = poww(primes[j][i], MOD-2);
        }
    }
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        char c; cin >> c;
        a[i] = 1-(c-'0');
    }

    for (int i = 1; i <= n; i++) {
        char c; cin >> c;
        b[i] = c-'0';
    }
    
    for (int i = 0; i < HASH_CNT; i++) {
        for (int j = 1; j <= n; j++) {
            ha[j].val[i] = (ha[j-1].val[i] + a[j] * primes[j][i]) % MOD;
            hb[j].val[i] = (hb[j-1].val[i] + b[j] * primes[j][i]) % MOD;
        }
    }

    int q; cin >> q;
    while (q--) {
        int x, y, l; cin >> x >> y >> l;
        x++; y++;
        int lo = 0, hi = l; // TODO check
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            if (H(ha, x, x+mid) == H(hb, y, y+mid)) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        if (lo == l) cout << "1 ";
        else cout << 1-b[y+lo] << " ";
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted12ms2612 KiB
2Accepted12ms2796 KiB
subtask240/40
3Accepted12ms3008 KiB
4Accepted12ms3096 KiB
5Accepted13ms3072 KiB
6Accepted12ms3596 KiB
7Accepted13ms3560 KiB
8Accepted14ms3616 KiB
9Accepted14ms3548 KiB
subtask30/30
10Accepted12ms3540 KiB
11Accepted32ms4456 KiB
12Runtime error14ms4640 KiB
13Runtime error14ms4760 KiB
14Runtime error17ms4808 KiB
15Runtime error17ms4936 KiB
16Runtime error17ms5176 KiB
17Runtime error17ms5264 KiB
subtask40/30
18Accepted18ms5168 KiB
19Runtime error17ms5264 KiB
20Runtime error14ms5388 KiB
21Runtime error16ms5492 KiB
22Runtime error17ms5704 KiB
23Runtime error17ms5896 KiB
24Runtime error17ms5980 KiB
25Runtime error17ms6076 KiB
26Runtime error17ms6164 KiB
27Runtime error17ms6160 KiB
28Runtime error17ms6280 KiB