Advertisement
leoanjos

Call from Mendes

Oct 25th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define long long long int
  6.  
  7. #define pii pair<int, int>
  8. #define pll pair<long, long>
  9. #define fst first
  10. #define snd second
  11.  
  12. #define all(ds) (ds).begin(), (ds).end()
  13. #define size(ds) (int) (ds).size()
  14.  
  15. #define pq priority_queue
  16. #define vi vector<int>
  17. #define pb push_back
  18. #define lb lower_bound
  19. #define ub upper_bound
  20.  
  21. const int MAX = 2e5 + 5;
  22.  
  23. map<int, string> word;
  24.  
  25. struct Trie {
  26. private:
  27.     int q;
  28.     vector<vi> trie;
  29.     vi index, smallest;
  30.  
  31. public:
  32.     Trie() {
  33.         q = 0;
  34.         index.assign(1, -1);
  35.         smallest.assign(1, -1);
  36.         trie.assign(1, vi(30, -1));
  37.     }
  38.  
  39.     int insert(string &s, int idx = 0, int node = 0) {
  40.         if (idx >= size(s)) {
  41.             word[++q] = s;
  42.             index[node] = q;
  43.  
  44.             if (smallest[node] == -1 || compare(s, word[smallest[node]]))
  45.                 smallest[node] = q;
  46.  
  47.             return smallest[node];
  48.         }
  49.        
  50.         if (trie[node][s[idx] - 'a'] == -1) {
  51.             trie[node][s[idx] - 'a'] = size(trie);
  52.             trie.pb(vi(30, -1));
  53.             smallest.pb(-1);
  54.             index.pb(-1);
  55.         }
  56.  
  57.         int nxt = insert(s, idx + 1, trie[node][s[idx] - 'a']);
  58.         if (smallest[node] == -1 || compare(word[nxt], word[smallest[node]]))
  59.             smallest[node] = nxt;
  60.  
  61.         return smallest[node];
  62.     }
  63.  
  64.     void remove(string &s, int idx = 0, int node = 0) {
  65.         if (idx >= size(s)) index[node] = -1, q++;
  66.         else remove(s, idx + 1, trie[node][s[idx] - 'a']);
  67.  
  68.         smallest[node] = index[node];
  69.         for (int i = 0; i < 26; i++) {
  70.             int n = trie[node][i];
  71.             if (n == -1) continue;
  72.  
  73.             if (smallest[node] == -1 || (smallest[n] != -1 && compare(word[smallest[n]], word[smallest[node]])))
  74.                 smallest[node] = smallest[n];
  75.         }
  76.     }
  77.  
  78.     int get_index(string &s) {
  79.         int idx = 0, node = 0; q++;
  80.         while (idx < size(s)) {
  81.             if (trie[node][s[idx] - 'a'] == -1)
  82.                 return -1;
  83.  
  84.             node = trie[node][s[idx++] - 'a'];
  85.         }
  86.  
  87.         return smallest[node];
  88.     }
  89.  
  90. private:
  91.     bool compare(string &s, string &t) {
  92.         return size(s) < size(t) || (size(s) == size(t) && s < t);
  93.     }
  94. };
  95.  
  96. int main() {
  97.     ios_base::sync_with_stdio(false);
  98.     cin.tie(NULL);
  99.  
  100.     Trie trie;
  101.     int Q; cin >> Q;
  102.  
  103.     while (Q--) {
  104.         int op; cin >> op;
  105.         if (op == 1) {
  106.             string X; cin >> X;
  107.             trie.insert(X);
  108.         } else if (op == 2) {
  109.             int X; cin >> X;
  110.             trie.remove(word[X]);
  111.         } else {
  112.             string X; cin >> X;
  113.  
  114.             int ans = trie.get_index(X);
  115.             cout << ans << "\n";
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement