Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include "pch.h"
  2. #include <math.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <set>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. struct Trie
  10. {
  11.     Trie *children[30];
  12.     bool is;
  13.  
  14.     Trie()
  15.     {
  16.         is = false;
  17.         for (int i = 0; i < 30; i++)
  18.             children[i] = NULL;
  19.     }
  20. };
  21.  
  22. void Insert(Trie *root, string key)
  23. {
  24.     Trie *temp = root;
  25.     for (auto &c : key)
  26.     {
  27.         if (temp->children[c - 'a'] == NULL)
  28.             temp->children[c - 'a'] = new Trie();
  29.         temp = temp->children[c - 'a'];
  30.     }
  31.     temp->is = true;
  32. }
  33.  
  34. bool Search(Trie *root, string key)
  35. {
  36.     Trie *temp = root;
  37.     for (auto &c : key)
  38.     {
  39.         if (temp->children[c - 'a'] == NULL)
  40.             return false;
  41.         temp = temp->children[c - 'a'];
  42.     }
  43.     if(temp->is == true) return true;
  44.     return false;
  45. }
  46.  
  47.  
  48.  
  49. int main()
  50. {
  51.     Trie *root = new Trie();
  52.     Insert(root, "abc");
  53.     Insert(root, "adc");
  54.     Insert(root, "bdc");
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement