constint N = 1e5 + 10; int son[N][26], cnt[N], idx;
// son buffer -> stores the row of child node // a d c a d e staticvoidbuild_trie(string &s) { int p = 0; for (int i = 0, len = s.size(); i < len; ++i) { int c = s[i] - 'a'; if (!son[p][c]) son[p][c] = ++idx; p = son[p][c]; } // 以p行结束(叶子节点的"子节点"行)的字符串计数 cnt[p]++; }
staticintquery(string &s) { int p = 0; for (int i = 0, len = s.size(); i < len; ++i) { int c = s[i] - 'a'; if (!son[p][c]) return0; p = son[p][c]; } return cnt[p]; }