#include #include #include #include #include using namespace std; ///数据结构 #define MAX 26 typedef struct Trie { Trie *next[MAX]; int v; //根据需要变化 }; Trie root; void createTrie(char *str) { int len = strlen(str); Trie *p = &root, *q; for(int i=0; inext[id] == NULL) { q = (Trie *)malloc(sizeof(Trie)); q->v = 1; //初始v==1 for(int j=0; jnext[j] = NULL; p->next[id] = q; p = p->next[id]; } else { p->next[id]->v++; p = p->next[id]; } } // p->v++; //p->v = -1; //若为结尾,则将v改成-1表示 } int findTrie(char *str) { int len = strlen(str); Trie *p = &root; for(int i=0; inext[id]; if(p == NULL) //若为空集,表示不存以此为前缀的串 return 0; // if(p->v == -1) //字符集中已有串是此串的前缀 // return -1; } return p->v; } int dealTrie(Trie* T) { int i; if(T==NULL) return 0; for(i=0;inext[i]!=NULL) { dealTrie(T->next[i]); T->next[i] = NULL; } } free(T); return 0; } int del(char *str, int val) { int len = strlen(str); Trie *p = &root; int id; for(int i =0 ; i < len; i++) { id = str[i] - 'a'; p->next[id]->v -= val; if(i != len-1) p = p->next[id]; } dealTrie(p->next[id]); p->next[id] = 0; } char in[100], str[100]; int main(void) { int n; scanf("%d", &n); for(int i = 0; i < 26; i++) root.next[i] = 0; while(n--) { scanf("%s%s", in, str); int len = strlen(str); if(in[0] == 'i') { createTrie(str); } else if(in[0] == 'd') { int val = findTrie(str); if(val) { del(str, val); } } else { int val = findTrie(str); if(val) printf("Yes\n"); else printf("No\n"); } // cout << ans << endl; } return 0; }