Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- const int alphabet_size = 26;
- struct node {
- node * children_of_node[alphabet_size];
- bool is_end_of_word;
- node() {
- is_end_of_word = false;
- for(int i = 0; i < alphabet_size; i++) {
- children_of_node[i] = NULL;
- }
- }
- };
- void insert_word(node * trie, string word) {
- node * at = trie;
- for(char c : word) {
- int pos = c - 'a';
- if(at -> children_of_node[pos] == NULL) {
- at -> children_of_node[pos] = new node();
- }
- at = at -> children_of_node[pos];
- }
- at -> is_end_of_word = true;
- }
- bool search_word(node * trie, string word) {
- node * at = trie;
- for(char c : word) {
- int pos = c - 'a';
- if(at -> children_of_node[pos] == NULL) {
- return false;
- }
- at = at -> children_of_node[pos];
- }
- return at -> is_end_of_word;
- }
- void delete_word(node * trie, string word) {
- node * at = trie;
- for(char c : word) {
- int pos = c - 'a';
- if(at -> children_of_node[pos] == NULL) {
- return;
- }
- at = at -> children_of_node[pos];
- }
- at -> is_end_of_word = false;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- node * trie = new node();
- while(true) {
- string a, b;
- cin >> a >> b;
- if(a == "insert") {
- insert_word(trie, b);
- }
- else if(a == "search") {
- cout << search_word(trie, b) << endl;
- }
- else {
- delete_word(trie, b);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement