#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include using namespace std; struct PrefixTreeNode { int wordPrefixedCount; bool existsExactWord; PrefixTreeNode* next[26]; PrefixTreeNode() { memset(this, 0, sizeof(PrefixTreeNode)); } bool insert(const char* word)//duplicated? { ++wordPrefixedCount; bool ret; if (*word != '\0') { if (!next[*word - 'a']) next[*word - 'a'] = new PrefixTreeNode(); ret = next[*word - 'a']->insert(word + 1); if (ret) --wordPrefixedCount; } else { ret = existsExactWord; existsExactWord = true; } return ret; } int deleteWord(const char* word) { int ret = 0; if (*word == '\0') { ret = wordPrefixedCount; memset(this, 0, sizeof(PrefixTreeNode)); } else if (next[*word - 'a']) { ret = next[*word - 'a']->deleteWord(word + 1); wordPrefixedCount -= ret; } return ret; } bool search(const char* word) { if (*word == '\0') return wordPrefixedCount > 0; if (!next[*word - 'a']) return false; return next[*word - 'a']->search(word + 1); } }; int main() { int T; cin >> T; PrefixTreeNode root; while (T--) { char op[10], word[32]; scanf("%s %s", op, word); switch (op[0]) { case 'i': root.insert(word); break; case 'd': root.deleteWord(word); break; case 's': printf(root.search(word) ? "Yes\n" : "No\n"); break; } } #ifndef ONLINE_JUDGE system("pause"); #endif return 0; }