QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#220477#5311. Master of BothbigJWA 11ms30476kbC++202.8kb2023-10-20 14:22:082023-10-20 14:22:09

Judging History

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

  • [2023-10-20 14:22:09]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:30476kb
  • [2023-10-20 14:22:08]
  • 提交

answer

#include <bits/stdc++.h>

template<typename P, typename Q> std::istream& operator>>(std::istream& is, std::pair<P, Q>& v) { std::cin >> v.first >> v.second; return is; }
template<typename P, typename Q> std::ostream& operator<<(std::ostream& os, std::pair<P, Q>& v) { std::cout << v.first << ' ' << v.second; return os; }
template<typename T, std::size_t N> std::istream& operator>>(std::istream& is, std::array<T, N>& v) { for (auto& i : v) is >> i; return is; }
template<typename T, std::size_t N> std::ostream& operator<<(std::ostream& os, std::array<T, N>& v) { for (auto& i : v) os << i << ' '; return os; }
template<typename T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& i : v) is >> i; return is; }
template<typename T> std::ostream& operator<<(std::ostream& os, std::vector<T>& v) { for (auto& i : v) os << i << ' '; return os; }
template<typename...Args> void debug(Args...args) { ((std::cerr << args << ' '), ...); std::cerr << '\n'; }
template<typename...Args> void println(Args...args) { ((std::cout << args << ' '), ...); std::cout << '\n'; }
template<typename P, typename Q> void chmax(P& a, Q b) { a = (b > a ? b : a); }
template<typename P, typename Q> void chmin(P& a, Q b) { a = (b < a ? b : a); }

using i64 = long long;

constexpr int A = 26, N = 5E5;
namespace T {
    int idx;
    int tr[N][A], cnt[N * A], stop[N * A];
    i64 pre[A][A];
    i64 oth = 0;

    int newNode() {
        ++idx;
        std::memset(tr[idx], 0, sizeof tr[idx]);
        cnt[idx] = 0;
        return idx;
    }

    void init() {
        idx = 0;
        oth = 0;
        std::memset(pre, 0, sizeof pre);
        newNode();
    }

    void add(const std::string& s) {
        int p = 1;
        for (auto c : s) {
            int x = c - 'a';
            if (!tr[p][x]) {
                tr[p][x] = newNode();
            }
            for (int i = 0; i < A; i++) {
                if (tr[p][i] != 0) {
                    pre[x][i]++;
                }
            }
            p = tr[p][x];
            if (stop[p] != 0) {
                oth++;
            }
            cnt[p]++;
        }
        stop[p]++;
    }
};
using namespace T;

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int n, q;
    std::cin >> n >> q;

    T::init();

    for (int i = 0; i < n; i++) {
        std::string s;
        std::cin >> s;
        T::add(s);
    }

    while (q--) {
        std::string s;
        std::cin >> s;
        i64 ans = 0;
        for (int i = 0; i < s.size(); i++) {
            for (int j = i + 1; j < s.size(); j++) {
                ans += pre[s[i] - 'a'][s[j] - 'a'];
            }
        }
        std::cout << ans + oth << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7948kb

input:

5 3
aac
oiputata
aaa
suikabudada
aba
abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm
aquickbrownfxjmpsvethlzydg

output:

4
3
4

result:

ok 3 number(s): "4 3 4"

Test #2:

score: -100
Wrong Answer
time: 11ms
memory: 30476kb

input:

100 100
spkfvrbkfsspmnlgrdojwdqutknvzejorqxsmfgbfrhpxkrrtravhmxenjrzypkxounrbantpkaezlcnudjmwxpgqakfoxcmdjcygujdtpluovbisxmklkzzuuyziapzyrszcggjkzrwmtyolnbobubbezdwmumyzyhaogiiolictzjpxbyaamecytpnyzxlumxjkzyfavxlzdwtgrxtqcnddzfocznitlaxlpcceuelqlbmyzetlpaivxnuvuctsbjbaulmbmkangqahpdojqimvmcugjeczkgx...

output:

964
1073
1010
992
1113
935
1041
1100
1001
986
1080
1109
1161
872
965
965
1105
1011
1028
959
1042
932
1026
1135
1105
1060
1037
983
997
1071
1142
990
1027
1003
981
1033
1025
986
1007
1027
1021
1094
989
1151
1023
1039
1037
983
1157
1018
975
987
1075
949
1008
970
982
1080
1055
1069
1120
954
1042
961
101...

result:

wrong answer 1st numbers differ - expected: '2368', found: '964'