constint N = 100010; int son[N][26], cnt[N], idx; char str[N]; int n;
voidinsert(char str[]) { int p = 0; for(int i = 0; str[i]; i ++) { int u = str[i] - 'a'; if( !son[p][u]) son[p][u] = ++ idx; p = son[p][u]; } cnt[p] ++; // 在 p 点结尾的单词数量加一 }
intquery(char str[]) { int p = 0; for(int i = 0; str[i]; i ++) { int u = str[i] - 'a'; if( !son[p][u]) return0; p = son[p][u]; } return cnt[p]; }
usingnamespace std; constint N = 100010; char str[N];
structNode { int cnt; Node* son[26]; Node () { cnt = 0; for(int i = 0; i < 26; i ++) son[i] = NULL; } } *root;
int n;
voidinsert(char str[]) { auto p = root; for(int i = 0; str[i]; i ++) { int u = str[i] - 'a'; if( !p->son[u]) p->son[u] = newNode(); p = p->son[u]; } p->cnt ++; }
intquery(char str[]) { auto p = root; for(int i = 0; str[i]; i ++) { int u = str[i] - 'a'; if( !p->son[u]) return0; p = p->son[u]; } return p->cnt; }