QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#371962#7789. Outro: True Love WaitscomeintocalmWA 1ms3568kbC++172.7kb2024-03-30 18:40:502024-03-30 18:40:50

Judging History

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

  • [2024-03-30 18:40:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3568kb
  • [2024-03-30 18:40:50]
  • 提交

answer

#include <bits/stdc++.h>

const long long P = 1e15 + 37;

std::unordered_map<unsigned long long, int> mp;

auto GetNext(std::string &s, const int n) -> std::pair<std::vector<int>, std::vector<int>> {
    std::vector<int> nxt(n);
    std::vector<int> d(n, 1);
    nxt[0] = -1;
    for (int i = 1, j = -1; i < n; ++i) {
        while (~j && s[i] != s[j + 1])
            j = nxt[j];
        if (s[i] == s[j + 1])
            ++j;
        nxt[i] = j;
        if (~j) {
            d[i] = d[j] + 1;
        }
    }
    return {nxt, d};
}

void GetMap(std::string &t) {
    int m = (int)t.length();
    for (int i = 0; i < m; ++i) {
        unsigned long long ha = 0;
        for (int j = i; j < m; ++j) {
            ha = (137ull * ha + (t[j] - 'a' + 1));
            ++mp[ha];
        }
    }
}

auto calc(std::string &s, std::string &t) -> long long {
    int n = (int)s.length();

    long long ret = 0;

    std::vector<unsigned long long> base(n);
    base[0] = 1;
    for (int i = 1; i < n; ++i)
        base[i] = 137ull * base[i - 1];
    for (int R = 0; R + 1 < n; ++R) {
        auto o = std::move(s.substr(R + 1, n));
        auto [nxt, dep] = std::move(GetNext(o, n - R));
        std::vector<int> d(R + 1);
        for (int i = 0, j = -1; i < R; ++i) {
            while (~j && (s[i] != o[j + 1] || j + 1 >= n - R)) {
                j = nxt[j];
            }
            if (s[i] == o[j + 1])
                ++j;
            if (~j) {
                // printf("i: %d  R: %d  j: %d\n", i, R, j);
                d[i] = dep[j];
            }
        }
        unsigned long long ha = 0;
        for (int i = R; i > 0; --i) {
            ha = (ha + base[R - i] * (s[i] - 'a' + 1));
            if (mp.find(ha) != mp.end()) {
                ret += 1ll * mp[ha] * d[i - 1];
            }
            // printf("hash %d  i:%d d[i-1]: %d\n", ha, i, d[i - 1]);
        }
    }

    return ret;
}

auto qwq(std::string &s, std::string &t) -> long long {
    int n = (int)s.length();
    int m = (int)t.length();

    long long ret = 0;

    for (int i = 0; i < n; ++i) {
        unsigned long long ha = 0;
        for (int j = i; j < n; ++j) {
            ha = (137ll * ha + (s[j] - 'a' + 1));
            if (mp.find(ha) != mp.end()) {
                ret += mp[ha];
            }
        }
    }
    return ret;
}

int main() {
    // freopen("d.in", "r", stdin);
    // freopen("d.out", "w", stdout);
    std::string s, t;
    std::cin >> s >> t;
    GetMap(t);
    long long ans = qwq(s, t);
    ans += calc(s, t);
    mp.clear();
    GetMap(s);
    ans += calc(t, s);
    // std::cout << calc(s, t) << ' ' << calc(t, s) << ' ' <<  qwq(s, t) << std::endl;
    std::cout << ans << std::endl;
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3568kb

input:

4
1 10 1
1 10 2
100 0 2
11 11 3

output:

0

result:

wrong answer 1st numbers differ - expected: '2', found: '0'