版本比较
[技巧] 「禁用哈希表」后也可以写出「线性空间」的字典树!
正文
增加 10 行删除 10 行
24 行未改动2526272829302930313233343435363738393940414142434445464748474849505151525354558 行未改动6465666768686970707172737423 行未改动24 行未改动int T, n, m; string str[N];
inline int id(char c) {return (('A' <= c && c <= 'Z') ? (c - 'A') : (('a' <= c && c <= 'z') ? (26 + c - 'a') : (52 + c - '0')));}ull tmp[L]; int cnt[L], idx; vector < int > son[L];// tmp 表示节点 i 的第 j 个字符是否有对应儿子(压位) // cnt 是结束标记,son 是邻接表 // tmp 表示节点 i 的第 j 个字符是否有对应儿子(压位)// cnt 是结束标记,son 是邻接表
inline void init(int pos){ // 对 cnt 数组做子树和 // 对 cnt 数组做子树和 for(int to : son[pos]) init(to), cnt[pos] += cnt[to];}inline int get_rank(int p, int c){ // 字符 c 算节点 p 的第几个儿子 // 字符 c 算节点 p 的第几个儿子 return __builtin_popcountll(((1ull << c) - 1) & tmp[p]) + 1; // 要写 popcountll,popcount 不能数 ull 中 1 的个数 // 要写 popcountll,popcount 不能数 ull 中 1 的个数}
inline void sol(){ cin >> n >> m; // 多测清空
// 多测清空 for(int i = 1; i <= idx; ++i) tmp[i] = cnt[i] = 0, son[i].clear(); idx = 1;
for(int i = 1; i <= n; ++i) { cin >> str[i]; int p = 1; for(char c : str[i])8 行未改动 p = son[p][get_rank(p, id(c)) - 1]; } ++cnt[p]; }
init(1);
for(int i = 1; i <= m; ++i) { string t; cin >> t; int p = 1; for(char c : t)23 行未改动