萌新刚学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 Kobe_Man @ 2024-08-07 17:31:57

@KAqwq 这都不会?不像我,都秒切的好吧


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

666膜拜棕名大佬


by KAqwq @ 2024-08-07 17:43:31

@Kobe_Man 老大说得对,帮我调一下/kk


by Ace_ace @ 2024-08-07 17:44:49

666膜拜棕名大佬


by Jerry_Money @ 2024-08-07 17:47:19

@Kobe_Man 膜拜棕名佬大


by AC_notonlyAC @ 2024-08-07 17:47:26

@Kobe_Man 属于是我们太蒟蒻了,拜见brown dalao


by keep_shining @ 2024-08-07 17:52:53

@KAqwq

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

typedef long long LL;
LL n, m, Trie[N * 30][65], tot, sum[N*30];//都要开3e6

char s[N*30];//这个也要开3e6

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 = 0; j <= 127; ++j) Trie[i][j] = 0;//j从0开始,因为你的s[i]-'A'若s[i]是'A',那么就会出现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 keep_shining @ 2024-08-07 17:54:20

@KAqwq 这样就可以 AC 了


by keep_shining @ 2024-08-07 17:55:28

膜拜棕名大佬%%%,我们还是太蒻了


by keep_shining @ 2024-08-07 17:57:00

楼主也好强%%%


| 下一页