Advertisement
coloriot

HA34

Dec 9th, 2024
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. class TrieNode {
  10. public:
  11.     array<int, 26> children;
  12.     int best_popularity;
  13.     int best_index;
  14.  
  15.     TrieNode() : best_popularity(-1), best_index(-1) {
  16.         children.fill(-1);
  17.     }
  18. };
  19.  
  20. class Trie {
  21. private:
  22.     vector<TrieNode> nodes;
  23.  
  24. public:
  25.     Trie() {
  26.         nodes.reserve(1000000);
  27.         // Начинаем с одного корня
  28.         nodes.push_back(TrieNode());
  29.     }
  30.  
  31.     void updateRootIfBetter(int popularity, int index) {
  32.         // Обновляем корень
  33.         if (popularity > nodes[0].best_popularity) {
  34.             nodes[0].best_popularity = popularity;
  35.             nodes[0].best_index = index;
  36.         }
  37.     }
  38.  
  39.     void insertWord(const string &word, int popularity, int index) {
  40.         int cur = 0;
  41.         for (char c : word) {
  42.             int nxt = c - 'a';
  43.             if (nodes[cur].children[nxt] == -1) {
  44.                 nodes[cur].children[nxt] = (int)nodes.size();
  45.                 nodes.push_back(TrieNode());
  46.             }
  47.             cur = nodes[cur].children[nxt];
  48.             // Обовляем лучший
  49.             if (popularity > nodes[cur].best_popularity) {
  50.                 nodes[cur].best_popularity = popularity;
  51.                 nodes[cur].best_index = index;
  52.             }
  53.         }
  54.     }
  55.  
  56.     int getChild(int node, char c) const {
  57.         if (node == -1) return -1;
  58.         return nodes[node].children[c - 'a'];
  59.     }
  60.  
  61.     int getBestIndex(int node) const {
  62.         if (node == -1) return -1;
  63.         return nodes[node].best_index;
  64.     }
  65. };
  66.  
  67.  
  68. int main() {
  69.     ios::sync_with_stdio(false);
  70.     cin.tie(NULL);
  71.  
  72.     int N, Q;
  73.     cin >> N >> Q;
  74.  
  75.     vector<string> words(N);
  76.     vector<int> popularity(N);
  77.     for (int i = 0; i < N; i++) {
  78.         cin >> words[i] >> popularity[i];
  79.     }
  80.  
  81.     Trie trie;
  82.  
  83.     for (int i = 0; i < N; i++) {
  84.         trie.updateRootIfBetter(popularity[i], i + 1);
  85.     }
  86.  
  87.     for (int i = 0; i < N; i++) {
  88.         trie.insertWord(words[i], popularity[i], i + 1);
  89.     }
  90.  
  91.     vector<int> prefix_stack;
  92.     prefix_stack.reserve(Q + 1);
  93.     prefix_stack.push_back(0);
  94.  
  95.     for (int i = 0; i < Q; i++) {
  96.         char op; cin >> op;
  97.         if (op == '+') {
  98.             char c; cin >> c;
  99.             int top_node = prefix_stack.back();
  100.             int nxt = trie.getChild(top_node, c);
  101.             prefix_stack.push_back(nxt);
  102.         } else {
  103.             prefix_stack.pop_back();
  104.         }
  105.  
  106.         int cur_node = prefix_stack.back();
  107.         int ans = trie.getBestIndex(cur_node);
  108.         cout << ans << "\n";
  109.     }
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement