QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#549008#7512. Almost Prefix Concatenationretired_midlightsWA 3ms18308kbC++143.0kb2024-09-05 23:52:282024-09-05 23:52:28

Judging History

你现在查看的是最新测评结果

  • [2024-09-05 23:52:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:18308kb
  • [2024-09-05 23:52:28]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)a; i <= (int)b; i ++)
#define per(i, a, b) for(int i = (int)a; i >= (int)b; i --)
#define ll long long
#define ld long double
using namespace std;

const int maxn = 1000010, base1 = 127, base2 = 131, mod1 = 1e9 + 7, mod2 = 1e9 + 9, mod = 998244353;

char s[maxn], t[maxn];
int n, m, power1[maxn], power2[maxn], hsh1_s[maxn], hsh1_t[maxn], hsh2_s[maxn], hsh2_t[maxn];
int f[maxn], g[maxn], h[maxn], suf_f[maxn], suf_g[maxn], suf_h[maxn];

int gethsh1_s(int l, int r) {
    return (hsh1_s[r] - 1ll * hsh1_s[l - 1] * power1[r - l + 1] % mod1 + mod1) % mod1;
}
int gethsh1_t(int l, int r) {
    return (hsh1_t[r] - 1ll * hsh1_t[l - 1] * power1[r - l + 1] % mod1 + mod1) % mod1;
}
int gethsh2_s(int l, int r) {
    return (hsh2_s[r] - 1ll * hsh2_s[l - 1] * power2[r - l + 1] % mod2 + mod2) % mod2;
}
int gethsh2_t(int l, int r) {
    return (hsh2_t[r] - 1ll * hsh2_t[l - 1] * power2[r - l + 1] % mod2 + mod2) % mod2;
}

bool check(int l1, int r1, int l2, int r2) {
    if(r1 > n || r2 > m) return 0;
    return gethsh1_s(l1, r1) == gethsh1_t(l2, r2) && gethsh2_s(l1, r1) == gethsh2_t(l2, r2);
}

int main() {
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    n = strlen(s + 1), m = strlen(t + 1);
    power1[0] = power2[0] = 1;
    rep(i, 1, max(n, m)) {
        power1[i] = 1ll * power1[i - 1] * base1 % mod1;
        power2[i] = 1ll * power2[i - 1] * base2 % mod2;
    }
    rep(i, 1, n) {
        hsh1_s[i] = (1ll * hsh1_s[i - 1] * base1 % mod1 + s[i] - 'a') % mod1;
        hsh2_s[i] = (1ll * hsh2_s[i - 1] * base2 % mod2 + s[i] - 'a') % mod2;
    }
    rep(i, 1, m) {
        hsh1_t[i] = (1ll * hsh1_t[i - 1] * base1 % mod1 + t[i] - 'a') % mod1;
        hsh2_t[i] = (1ll * hsh2_t[i - 1] * base2 % mod2 + t[i] - 'a') % mod2;
    }
    suf_f[n + 1] = 1;
    per(i, n, 1) {
        int l = i, r = min(n, i + m - 1), pos = min(n + 1, i + m);
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(check(i, mid, 1, mid - i + 1)) l = mid + 1;
            else r = mid - 1, pos = mid;
        }
        // printf("i : %d pos : %d\n", i, pos);
        int tmp = pos + 1;
        l = tmp, r = min(n + 1, i + m - 1);
        pos = min(n + 1, i + m);
        while(l <= r) {
            int mid = (l + r) >> 1;
            if(check(tmp, mid, tmp - i + 1, mid - tmp + 1 + tmp - i)) l = mid + 1;
            else r = mid - 1, pos = mid;
        }
        // printf("i : %d pos : %d\n", i, pos);
        pos ++;
        f[i] = (suf_f[i + 1] - suf_f[pos] + mod) % mod;
        g[i] = ((suf_g[i + 1] - suf_g[pos] + mod) % mod + f[i]) % mod;
        h[i] = ((suf_h[i + 1] - suf_h[pos] + mod) % mod + 2 * g[i] % mod - f[i]) % mod;
        suf_f[i] = (suf_f[i + 1] + f[i]) % mod;
        suf_g[i] = (suf_g[i + 1] + g[i]) % mod;
        suf_h[i] = (suf_h[i + 1] + h[i]) % mod;
        // printf("i : %d f : %d\n", i, f[i]);
    }
    printf("%d\n", h[1]);
    return 0;
}
/*
ababaab
aba

ac
ccpc
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16216kb

input:

ababaab
aba

output:

473

result:

ok 1 number(s): "473"

Test #2:

score: 0
Accepted
time: 0ms
memory: 18124kb

input:

ac
ccpc

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 3ms
memory: 18308kb

input:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

75038697

result:

ok 1 number(s): "75038697"

Test #4:

score: 0
Accepted
time: 0ms
memory: 18184kb

input:

lvvvllvllvllvllllllllvvvllvlllvvlvlvllvlvvlvvvvlvvllllllvvlvlvvlllvvlvlvllllllvlvvvvvvlllvvvllvlvvvlvvlllvvvvvvlvlllvvvvlvvvvvlvvlvvlllvvllvvllvlvlvlvlvllllvvllvvllvlllvvvllllvvlvvllvvvvlvlvvlvvlllvvvvvvvvlvvlvlllvllvvvvllvvvlvvvvvvlvlllvllllvllllllllvvllllllvlvvlvvvlvllllvllvlvvllllllvlvvvlvlvlvvvl...

output:

538419149

result:

ok 1 number(s): "538419149"

Test #5:

score: -100
Wrong Answer
time: 0ms
memory: 18192kb

input:

fzztyyyfztzzfzyztftyfzyyzzzztyyfzttzttztyzztyyyfyyftyfyfzzffyzffytttzttyzzftyfyfyftyyfzyzffyfyyzztzyyttyfyztfyfzyfzfzyftttfyyfyytzyyzfyyyzztfttzyyytzzffytyzyyyyfzfftftzzztyfftfzfzytftfttytfyzfytzfzztttttzzyztyftzzzfzfzfffttyztzfftfftyfyffztzyffttyyfyfzytytyyttfzzfyyytzzftzyyfftftyytyffzffztfytfyyyty...

output:

-51871165

result:

wrong answer 1st numbers differ - expected: '867833603', found: '-51871165'