Dsxv

Trie_sample_q

Jul 10th, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. const int N = 1e6 + 10 ;
  5. vector<vector<int>> Trie;
  6. int val[N] ;
  7. void insert(string s , int x){
  8.     int cur = 0 ;
  9.     for(auto i : s){
  10.         if(Trie[cur][i-'a'] == 0){
  11.             Trie[cur][i-'a'] = Trie.size() ;
  12.             Trie.push_back(vector<int>(26)) ;
  13.         }
  14.         cur = Trie[cur][i-'a'] ;
  15.         val[cur] = max(val[cur] , x) ;
  16.     }
  17. }
  18.  
  19. int main(){
  20.     ios :: sync_with_stdio(0) ;
  21.     cin.tie(0) ;
  22.     int n , m ;
  23.     cin >> n >> m;
  24.  
  25.     Trie.push_back(vector<int>(26)) ;
  26.     for(int i = 0 ; i < n ; i++){
  27.         string s ; int x ;
  28.         cin >> s >> x ;
  29.         insert(s,x) ;
  30.     }
  31.     while(m--){
  32.         string s ;
  33.         cin >> s ;
  34.         int cur = 0 ;
  35.         bool found = true ;
  36.         int mx = INT_MAX ;
  37.         for(auto i : s){
  38.             cur = Trie[cur][i-'a'] ;
  39.             if(cur == 0){
  40.                 found = false ;
  41.                 break ;
  42.             }
  43.             mx = min(mx , val[cur]) ;
  44.         }
  45.         if(!found) cout << -1 << '\n' ;
  46.         else cout << mx << '\n' ;
  47.     }
  48. }
Add Comment
Please, Sign In to add comment