Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- 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 * tmp = trie;
- for(int i = 0; i < (int) word.size(); i++) {
- int c = word[i] - 'a';
- if(tmp -> children_of_node[c] == NULL) {
- tmp -> children_of_node[c] = new node();
- }
- tmp = tmp -> children_of_node[c];
- }
- tmp -> is_end_of_word = true;
- }
- bool search_word(node * trie, string word) {
- node * tmp = trie;
- for(int i = 0; i < (int) word.size(); i++) {
- int c = word[i] - 'a';
- if(tmp -> children_of_node[c] == NULL) {
- return false;
- }
- tmp = tmp -> children_of_node[c];
- }
- return tmp -> is_end_of_word;
- }
- void delete_word(node * trie, string word) {
- node * tmp = trie;
- for(int i = 0; i < (int) word.size(); i++) {
- int c = word[i] - 'a';
- if(tmp -> children_of_node[c] == NULL) {
- return;
- }
- tmp = tmp -> children_of_node[c];
- }
- tmp -> is_end_of_word = false;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- node * trie = new node();
- while(true) {
- string s;
- cin >> s;
- if(s == "insert") {
- string a;
- cin >> a;
- insert_word(trie, a);
- }
- else if(s == "search") {
- string a;
- cin >> a;
- cout << search_word(trie, a) << endl;
- }
- else {
- string a;
- cin >> a;
- delete_word(trie, a);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement