#include #include const int maxc = 3000005; int n; struct trie { char ch[maxc]; int head[maxc], next[maxc]; int val[maxc]; int nodes; trie(): nodes(0) { memset(ch, 0, sizeof ch); memset(head, 0, sizeof head); memset(next, 0, sizeof next); } void insert(char *c) { int cur = 0; for (int i = 0; c[i] != '\0'; i++) { int nex; for (nex = head[cur]; nex; nex = next[nex]) if (ch[nex] == c[i]) break; if (!nex) { nex = ++nodes; ch[nex] = c[i]; next[nex] = head[cur]; head[cur] = nex; val[nex] = 1; } cur = nex; val[cur]++; } } void remove(char *c) { int cur = 0; for (int i = 0; c[i] != '\0'; i++) { int nex; for (nex = head[cur]; nex; nex = next[nex]) if (ch[nex] == c[i]) break; if (!nex) return; cur = nex; } ch[cur] = '$'; update(c, val[cur]); } void update(char *c, int d) { int cur = 0; for (int i = 0; c[i] != '\0'; i++) { int nex; for (nex = head[cur]; nex; nex = next[nex]) if (ch[nex] == c[i]) break; cur = nex; val[cur] -= d; } } bool search(char *c) { int cur = 0; for (int i = 0; c[i] != '\0'; i++) { int nex; for (nex = head[cur]; nex; nex = next[nex]) if (ch[nex] == c[i]) break; if (!nex) return false; cur = nex; } return val[cur] > 0 ? true : false; } void print() { for (int i = 0; i <= nodes; i++) printf("%c ", ch[i]); printf("\n"); printf("next[]="); for (int i = 0; i <= nodes; i++) printf("%d ", next[i]); printf("\n"); printf("head[]="); for (int i = 0; i <= nodes; i++) printf("%d ", head[i]); printf("\n"); } }; trie sol; int main() { // freopen("C.in", "r", stdin); // freopen("C.out", "w", stdout); scanf("%d", &n); char ch1[10], ch2[40]; while (n--) { scanf("%s%s", ch1, ch2); // printf("Execute %s %s\n", ch1, ch2); switch (ch1[0]) { case 'i': sol.insert(ch2); break; case 'd': sol.remove(ch2); break; case 's': printf(sol.search(ch2) ? "Yes\n" : "No\n"); break; } // printf("Current Trie\n"); // sol.print(); } return 0; }