萌新刚学oi16pts求调

P8306 【模板】字典树

KAqwq @ 2024-08-07 17:15:16

#include <bits/stdc++.h>
#define N 100005

typedef long long LL;
LL n, m, Trie[N * 30][65], tot, sum[N];

char s[N];

inline void insert() {
    LL len = strlen(s + 1), p = 1;
    for (int i = 1; i <= len; ++i) {
        LL ch = 0;
        if (s[i] >= 'A' && s[i] <= 'Z') ch = s[i] - 'A';
        else if (s[i] >= 'a' && s[i] <= 'z') ch = s[i] - 'a' + 26;
        else ch = s[i] - '0' + 52;
        if (!Trie[p][ch]) Trie[p][ch] = ++tot;
        p = Trie[p][ch];
        ++sum[p];
    }
}
inline LL query() {
    LL len = strlen(s + 1), p = 1;
    for (int i = 1; i <= len; ++i) {
        LL ch = 0;
        if (s[i] >= 'A' && s[i] <= 'Z') ch = s[i] - 'A';
        else if (s[i] >= 'a' && s[i] <= 'z') ch = s[i] - 'a' + 26;
        else ch = s[i] - '0' + 52;
        if (!Trie[p][ch]) return 0;
        p = Trie[p][ch];
    }
    return sum[p];
}
LL T;
int main() {
    std::cin >> T;
    while (T--) {
        for (LL i = 1; i <= tot; ++i) for (LL j = 1; j <= 127; ++j) Trie[i][j] = 0;
        for (LL i = 1; i <= tot; ++i) sum[i] = 0;
        tot = 1;
        std::cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            scanf("%s", s + 1);
            insert();
        }
        for (int i = 1; i <= m; ++i) {
            scanf("%s", s + 1);
            std::cout << query() << '\n';
        }
    }
    return 0;
}

by KAqwq @ 2024-08-07 17:57:13

@keep_shining thx,此帖结。


by LK_YYds @ 2024-08-07 17:57:40

都膜拜一遍%%%


by danlao @ 2024-08-07 17:57:55

@Kobe_Man 刚刚看了洛谷帖子保存站,你已经发了好几次这样的语句,判定为被jc,请保管好你的账号


上一页 |