Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
using namespace std; const int ALPHABET_SIZE = 26; struct node { node *children_of_this_node[ALPHABET_SIZE]; bool end_of_word; node() { end_of_word = false; for(int i = 0; i < ALPHABET_SIZE; i++) { children_of_this_node[i] = NULL; } } }; void insert_word(node * trie, string s) { node * tmp = trie; for(int i = 0; i < (int) s.size(); i++) { int current_char = (s[i] - 'a'); if(tmp -> children_of_this_node[current_char] == NULL) { tmp -> children_of_this_node[current_char] = new node(); } tmp = tmp -> children_of_this_node[current_char]; } tmp -> end_of_word = true; } bool search_word(node * trie, string s) { node * tmp = trie; for(int i = 0; i < (int) s.size(); i++) { int current_char = (s[i] - 'a'); if(tmp -> children_of_this_node[current_char] == NULL) { return false; } tmp = tmp -> children_of_this_node[current_char]; } return tmp -> end_of_word; } void delete_word(node * trie, string s) { node * tmp = trie; for(int i = 0; i < (int) s.size(); i++) { int current_char = (s[i] - 'a'); if(tmp -> children_of_this_node[current_char] == NULL) { return; } tmp = tmp -> children_of_this_node[current_char]; } tmp -> end_of_word = false; } int main() { int n; cin >> n; node * trie = new node(); vector
dictionary(n); for(int i = 0; i < n; i++) { cin >> dictionary[i]; insert_word(trie, dictionary[i]); } int q; cin >> q; for(int i = 0; i < q; i++) { char c; cin >> c; if(c == 'D') { string s; cin >> s; delete_word(trie, s); } else { string s; cin >> s; if(search_word(trie, s)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)