/* -*- coding: utf-8 -*- * * 3667.cc: No.3667 Prefix Count Queries - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_QN = 200000; /* typedef */ struct Node { Node *cs[26], *p; int f, ci; Node(Node *_p = nullptr): cs(), p(_p), f(), ci() {} }; /* global variables */ char s[MAX_N + 4]; Node *root; /* subroutines */ void trie_add(char s[]) { Node *u = root; for (int i = 0; s[i]; i++) { int si = s[i] - 'a'; if (u->cs[si] == nullptr) u->cs[si] = new Node(u); u = u->cs[si]; } u->f++; } void trie_setall() { for (Node *u = root; u != nullptr;) { if (u->ci < 26) { auto v = u->cs[u->ci++]; if (v != nullptr) { u = v; } } else { if (u->p != nullptr) u->p->f += u->f; u = u->p; } } } /* main */ int main() { int n; scanf("%d", &n); root = new Node(); for (int i = 0; i < n; i++) { scanf("%s", s); trie_add(s); } trie_setall(); int qn; scanf("%d", &qn); Node *u = root; while (qn--) { int op; scanf("%d", &op); if (op == 1) { char xs[4]; scanf("%s", xs); int x = xs[0] - 'a'; if (u->cs[x] == nullptr) u->cs[x] = new Node(u); u = u->cs[x]; } else if (op == 2) { u = u->p; } else { printf("%d\n", u->f); } } return 0; }