#include #include #include using namespace std; #define MAXL 27 #define MAXO 8 #define MAXA 32 #define BASE 'a' typedef struct _A { struct _A *next[MAXL]; }A; A *root; void init() { root = new A(); } void insertString(char *word) { A *p = root; for (int i = 0, index; word[i] != '\0'; ++i) { index = word[i] - BASE; if (p->next[index] == NULL){ p->next[index] = new A(); } p = p->next[index]; } } A* findString(char *word) { A *p = root; for (int i = 0, index; word[i] != '\0'; ++i) { index = word[i] - BASE; if (p->next[index] == NULL) { return NULL; } p = p->next[index]; } return p; } void deleteLeafs(A *rt) { for (int i = 0; i < MAXL; ++i) { if (rt->next[i] != NULL) { deleteLeafs(rt->next[i]); rt->next[i] = NULL; } } delete rt; } void deletePath(A *pre, A *rt, char *word, bool &flag) { if (word[0] == '\0'){ for (int i = 0; i < MAXL; ++i) { if (pre->next[i] == rt){ delete rt; pre->next[i] = NULL; break; } } return; } int index = word[0] - BASE; deletePath(rt, rt->next[index], word+1, flag); if (!flag){ int i; for (i = 0; i < MAXL; ++i) { if (rt->next[i] != NULL) { flag = true; break; } } if (!flag) { for (int i = 0; i < MAXL; ++i) { if (pre->next[i] == rt) { pre->next[i] = NULL; break; } } delete rt; } } } void deleteString(char *word) { A *p = findString(word); if (p != NULL) { for (int i = 0; i < MAXL; ++i){ if (p->next[i] != NULL){ deleteLeafs(p->next[i]); p->next[i] = NULL; } } bool flag = false; deletePath(root, root->next[word[0]-BASE], word+1, flag); } } void solve(char *op, char *word) { if (strcmp(op, "insert")==0) { insertString(word); } else if (strcmp(op, "delete")==0) { deleteString(word); } else if (strcmp(op, "search")==0) { if (findString(word) != NULL) { puts("Yes"); } else { puts("No"); } } } int main() { int n; char op[MAXO], word[MAXA]; scanf ("%d", &n); init(); while (n--) { scanf (" %s%s", op, word); solve(op, word); } return 0; }