社区讨论

样例本地过IDE RE求调

P8306【模板】字典树参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mdfrk7ra
此快照首次捕获于
2025/07/23 17:31
7 个月前
此快照最后确认于
2025/11/04 03:52
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
#define fil [[gnu::always_inline]] inline
#define up(i,l,r) for(long long i=(l),E##i=(r);i<=E##i;++i)
#define dn(i,r,l) for(long long i=(r),E##i=(l);i>=E##i;--i)
using namespace std; typedef long long ll;
constexpr ll MAXN = 5 + 1e5;
#define LOCAL 0

ll n;
struct Trie {
    vector<array<ll, 128>> t;
    vector<int> b; // step over
    ll rt, cnt;

    ll newnd() {
        t.emplace_back();
        b.emplace_back();
        return cnt++;
    }
    void build() {
        rt = newnd();
    }
    void insert(string const& s) {
        static ll p, tmp;
        p = rt; ++b[p];
        for (char i : s) {
            if (!t[p][i]) {
                tmp = newnd();
                t[p][i] = tmp;
            }
            p = t[p][i];
            ++b[p];
        }
    }
    ll find(string const& s) {
        static ll p;
        p = rt;
        for (char i : s) {
            if (!t[p][i]) return 0;
            p = t[p][i];
        }
        return b[p];
    }
};

void go() {
    ll q; string s;
    Trie tr;

    cin >> n >> q;
    tr.build();
    up (i, 1, n) {
        cin >> s;
        tr.insert(s);
    }
    while (q--) {
        cin >> s;
#if LOCAL
        cout << "|" << tr.find(s) << endl;
#else
        cout << tr.find(s) << endl;
#endif
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    ll T;
    cin >> T;
    while (T--) go();
}

回复

3 条回复,欢迎继续交流。

正在加载回复...