样例本地过IDE RE求调
查看原帖
样例本地过IDE RE求调
1476288
lyb_qhd楼主2025/7/23 17:30
#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();
}

2025/7/23 17:30
加载中...