Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std ;
- const int N = 1e6 + 10 ;
- vector<vector<int>> Trie;
- int val[N] ;
- void insert(string s , int x){
- int cur = 0 ;
- for(auto i : s){
- if(Trie[cur][i-'a'] == 0){
- Trie[cur][i-'a'] = Trie.size() ;
- Trie.push_back(vector<int>(26)) ;
- }
- cur = Trie[cur][i-'a'] ;
- val[cur] = max(val[cur] , x) ;
- }
- }
- int main(){
- ios :: sync_with_stdio(0) ;
- cin.tie(0) ;
- int n , m ;
- cin >> n >> m;
- Trie.push_back(vector<int>(26)) ;
- for(int i = 0 ; i < n ; i++){
- string s ; int x ;
- cin >> s >> x ;
- insert(s,x) ;
- }
- while(m--){
- string s ;
- cin >> s ;
- int cur = 0 ;
- bool found = true ;
- int mx = INT_MAX ;
- for(auto i : s){
- cur = Trie[cur][i-'a'] ;
- if(cur == 0){
- found = false ;
- break ;
- }
- mx = min(mx , val[cur]) ;
- }
- if(!found) cout << -1 << '\n' ;
- else cout << mx << '\n' ;
- }
- }
Add Comment
Please, Sign In to add comment