Advertisement
Goga21

Untitled

Sep 3rd, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. //#define int long long
  4.  
  5. using namespace std;
  6.  
  7. struct node{
  8.     node *next[26];
  9.     int cnt;
  10.  
  11.     node(){
  12.         for(int i = 0; i < 26; ++i){
  13.             next[i] = nullptr;
  14.         }
  15.         cnt = 0;
  16.     }
  17. };
  18.  
  19. node *root = new node();
  20.  
  21. void add(string& input){
  22.     node *curr = root;
  23.  
  24.     for(int i = 0; i < input.size(); ++i){
  25.         if(curr->next[input[i] - 'a'] == nullptr){
  26.             curr->next[input[i] - 'a'] = new node();
  27.         }
  28.  
  29.         curr = curr->next[input[i] - 'a'];
  30.         curr->cnt++;
  31.     }
  32. }
  33.  
  34. int search(string &input){
  35.     int ans = 0;
  36.  
  37.     node *temp = root;
  38.     for(auto el : input){
  39.         ans += temp->cnt;
  40.         temp = temp->next[el - 'a'];
  41.     }
  42.  
  43.     return ans;
  44. }
  45.  
  46. signed main() {
  47.     ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  48.     int n, m;
  49.     cin >> n;
  50.  
  51.     vector<string> v(n);
  52.     map<string, int> input;
  53.  
  54.     for(int i = 0; i < n; ++i) cin >> v[i];
  55.  
  56.     cin >> m;
  57.  
  58.     for(int i = 0; i < m; ++i){
  59.         string s;
  60.         cin >> s;
  61.         input[s] = 1;
  62.     }
  63.  
  64.     for(auto el : v){
  65.         add(el);
  66.         if(input[el] == 1){;
  67.             input[el] = search(el);
  68.         }
  69.     }
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement