QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#315722#7512. Almost Prefix Concatenationreal_sigma_team#WA 2ms19224kbC++232.9kb2024-01-27 15:29:302024-01-27 15:29:30

Judging History

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

  • [2024-01-27 15:29:30]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:19224kb
  • [2024-01-27 15:29:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

int mod = 998244353;

int add(int a, int b) {
    return a + b >= mod ? a + b - mod : a + b;
}

int sub(int a, int b) {
    return a < b ? a + mod - b : a - b;
}

int mul(int a, int b) {
    return 1ll * a * b % mod;
}

const int N = 1e6 + 10;

uint64_t base = 29;
uint64_t pw[N];
uint64_t base2 = 37;
uint64_t pw2[N];

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    pw[0] = 1;
    for (int i = 1; i < N; ++i) pw[i] = pw[i - 1] * base;
    pw2[0] = 1;
    for (int i = 1; i < N; ++i) pw2[i] = pw2[i - 1] * base;

    string s, t;
    cin >> s >> t;
    int n = s.size();
    int m = t.size();
    vector<uint64_t> hs(n + 1), ht(m + 1);
    for (int i = 0; i < n; ++i) {
        hs[i + 1] = hs[i] * base + uint64_t(s[i] - 'a' + 1);
    }
    for (int i = 0; i < m; ++i) {
        ht[i + 1] = ht[i] * base + uint64_t(t[i] - 'a' + 1);
    }
    vector<uint64_t> hs2(n + 1), ht2(m + 1);
    for (int i = 0; i < n; ++i) {
        hs2[i + 1] = hs2[i] * base2 + uint64_t(s[i] - 'a' + 1);
    }
    for (int i = 0; i < m; ++i) {
        ht2[i + 1] = ht2[i] * base2 + uint64_t(t[i] - 'a' + 1);
    }
    auto gets = [&](int l, int r) -> int {
        return hs[r + 1] - hs[l] * pw[r - l + 1];
    };
    auto gett = [&](int l, int r) -> int {
        return ht[r + 1] - ht[l] * pw[r - l + 1];
    };
    auto gets2 = [&](int l, int r) -> int {
        return hs2[r + 1] - hs2[l] * pw2[r - l + 1];
    };
    auto gett2 = [&](int l, int r) -> int {
        return ht2[r + 1] - ht2[l] * pw2[r - l + 1];
    };
    vector<int> d_sum_sq(n + 1);
    vector<int> d_sum(n + 1);
    vector<int> d_cnt(n + 1);
    d_cnt[1] = sub(d_cnt[1], 1);
    int sum_sq = 0, sum = 0, cnt = 1;
    for (int i = 0; i <= n; ++i) {
        sum_sq = add(sum_sq, d_sum_sq[i]);
        sum = add(sum, d_sum[i]);
        cnt = add(cnt, d_cnt[i]);
        if (i == n) {
            cout << sum_sq;
            return 0;
        }
        int L = i - 1, R = n;
        while (R - L > 1) {
            int M = (L + R) / 2;
            if (M - i < m && gets(i, M) == gett(0, M - i) && gets2(i, M) == gett2(0, M - i)) L = M;
            else R = M;
        }
        int pos = L + 1;
        L = pos, R = n;
        while (R - L > 1) {
            int M = (L + R) / 2;
            if (M - i < m && gets(pos + 1, M) == gett(pos + 1 - i, M - i) && gets2(pos + 1, M) == gett2(pos + 1 - i, M - i)) L = M;
            else R = M;
        }
        R = min(R, i + m);
        int add_sq = add(sum_sq, add(mul(2, sum), cnt));
        int add_sum = add(sum, cnt);
        int add_cnt = cnt;
        sum_sq = add(sum_sq, add_sq);
        sum = add(sum, add_sum);
        cnt = add(cnt, add_cnt);
        if (R < n) {
            d_sum_sq[R + 1] = sub(d_sum_sq[R + 1], add_sq);
            d_sum[R + 1] = sub(d_sum[R + 1], add_sum);
            d_cnt[R + 1] = sub(d_cnt[R + 1], add_cnt);
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 19224kb

input:

ababaab
aba

output:

110

result:

wrong answer 1st numbers differ - expected: '473', found: '110'