warriorscats

Trie

Jun 1st, 2020
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <algorithm>
  5.  
  6.  
  7. struct Node {
  8.     std::string key;
  9.     Node* desc[5];
  10.  
  11.     Node (std::string s): key (s) {
  12.         for (int i = 0; i < 5; ++ i)
  13.             desc[i] = nullptr;
  14.     }
  15. };
  16.  
  17. int n, m, task, dim;
  18. std::string s ("ROOT"), temp;
  19. Node* Root = new Node (s);
  20. std::map <std::string, bool> Hash;
  21.  
  22.  
  23. inline void Insert (Node*& root, std::string s, std::string temp) {
  24.     if (!s.empty ()) {
  25.         if (!root) root = new Node (temp);
  26.         std::cout << root->key << " ";
  27.         temp += s.back ();
  28.         char aux = s.back (); s.pop_back ();
  29.          Insert (root->desc[aux - '0'], s, temp);
  30.     } else root = new Node (temp), std::cout << root->key << " ";
  31. }
  32.  
  33. inline bool hasMoreSons (Node*& root) {
  34.     return root->desc[1] || root->desc[2] || root->desc[3] ||root->desc[4];
  35. }
  36.  
  37. inline void Delete (Node*& root, std::string s) {
  38.     std::cout << root->key << " ";
  39.     if (!s.empty ()) {
  40.         char aux = s.back (); s.pop_back ();
  41.         //std::cout << root->key << " ";
  42.         if (root->desc[aux - '0']) Delete (root->desc[aux - '0'], s);
  43.         if (!hasMoreSons (root) && root->key != "ROOT") delete root;
  44.     } else delete root;
  45. }
  46.  
  47. inline int Search (Node*& root, std::string s, std::string temp) {
  48.     //std::cout << root->key << " ";
  49.     if (hasMoreSons (root)) {
  50.         temp += s.back ();
  51.         std::cout << root->key << " ";
  52.         char aux = s.back (); s.pop_back ();
  53.          if (root->desc[aux - '0']) return Search (root->desc[aux - '0'], s, temp);
  54.          else return dim >> (2 * (int)temp.size ());
  55.      } else return dim;
  56. }
  57.  
  58. inline void R1234 (Node*& root) {
  59.     if (root) {
  60.         std::cout << root->key << " ";
  61.         for (int i = 1; i <= 4; ++ i)
  62.             R1234 (root->desc[i]);
  63.     }
  64. }
  65.  
  66.  
  67. int main () {
  68.     std::cin >> n >> m;
  69.     dim = 1 << (2 * __builtin_ctz (n));
  70.     for (int i = 1; i <= m; ++ i) {
  71.         std::cin >> task >> s;
  72.         if (task == 1) {
  73.             std::reverse (s.begin (), s.end ());
  74.             if (Hash[s]) Delete (Root, s);
  75.             else Insert (Root, s, temp);
  76.             std::cout << "\n";
  77.             std::reverse (s.begin (), s.end ());
  78.             Hash[s] = !Hash[s];
  79.             //std::cout << Root->key << "\n";
  80.             //R1234 (root); std::cout << "\n";
  81.         } else {
  82.             if (Hash[s]) std::cout << "0\n";
  83.             else std::reverse (s.begin (), s.end ()), std::cout << Search (Root, s, temp) << "\n";
  84.         }
  85.     }
  86.  
  87.     return 0;
  88. }
Add Comment
Please, Sign In to add comment