求助pbds如何解决本题

P8306 【模板】字典树

FXJFXJ @ 2024-06-03 18:32:40

#include <bits/stdc++.h>
using namespace std;
#include<bits/extc++.h>
using namespace __gnu_pbds;
using pref_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;
void solve() {

    int n, q;cin >> n >> q;

    pref_trie tr;
    for (int i = 1;i <= n;i++) {
        string s;cin >> s;
        tr.insert(s);
    }
    while (q--) {
        string s;cin >> s;
        auto range = tr.prefix_range(s);
        int t = 0;
        for (auto it = range.first;it != range.second;it++, t++);
        cout << t << '\n';
    }
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _ = 1;
    cin >> _;
    while (_--)
        solve();
}

按照正常字典树写法写出来会TLE,那么有什么办法使得不会T呢?


|