QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#700610#5311. Master of BothHe_ng#ML 0ms0kbC++201.5kb2024-11-02 13:13:032024-11-02 13:13:05

Judging History

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

  • [2024-11-02 13:13:05]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:0kb
  • [2024-11-02 13:13:03]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5 * 1e6 + 10;
int pr[26][26]; // 因为前i后j而产生差异的字符串数量
struct Trie {
    Trie(int n) : son(N), cnt(N), nidx(0) {}
    void insert(const vector<string> &vs) {
        for (int k = 0; k < vs.size(); k++) {
            int p = 0;
            for (int i = 0; i < vs[k].size(); i++) {
                int u = vs[k][i] - 'a';
                if (!son[p][u]) {
                    son[p][u] = ++nidx;
                }
                for (int j = 0; j < 26; j++) {
                    pr[j][u] += cnt[son[p][j]];
                }
                cnt[son[p][u]] += 1;
                p = son[p][u];
            }
        }
    }

    int nidx;
    vector<array<int, 26>> son;
    vector<int> cnt;
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<string> vs(n);
    Trie tr(n);
    for (int i = 0; i < n; i++) {
        cin >> vs[i];
    }
    tr.insert(vs);
    string pp;
    // for (int i = 0; i < 26;i++){
    //     for (int j = 0; j < 26;j++){
    //         cout << (char)('a' + i) << " " << (char)('a' + j) << " " << pr[i][j] << endl;
    //     }
    // }
    while (q--) {
        cin >> pp;
        int ans = 0;
        for (int i = 0; i < 26; i++) {
            for (int j = i + 1; j < 26; j++) {
                ans += pr[pp[j] - 'a'][pp[i] - 'a'];
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Memory Limit Exceeded

input:

5 3
aac
oiputata
aaa
suikabudada
aba
abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm
aquickbrownfxjmpsvethlzydg

output:

4
3
4

result: