Advertisement
Salvens

E

Aug 10th, 2023
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <random>
  7. #include <chrono>
  8. #include <set>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. //#define int long long
  14. #pragma comment(linker,"/STACK:1000000000,1000000000")
  15.  
  16. const long long INF = 1e9 + 7;
  17. const int MAXN = 26 * 100000 + 100;
  18. const int N = 1e5 + 10;
  19.  
  20. struct vertex {
  21.     int next[26];
  22.     int terminal = 0;
  23. } v[MAXN];
  24.  
  25. int root = 0, top = 1;
  26. array<long long, MAXN> dp;
  27.  
  28. void add(const string& s) {
  29.     int cur = root;
  30.     for (auto& i: s) {
  31.         int x = i - 'a';
  32.         if (v[cur].next[x] == 0) {
  33.             v[cur].next[x] = top++;
  34.         }
  35.         cur = v[cur].next[x];
  36.         ++dp[cur];
  37.     }
  38.     v[cur].terminal = 1;
  39. }
  40.  
  41. long long  get(const string& s) {
  42.     int cur = root;
  43.     for (auto& i: s) {
  44.         int x = i - 'a';
  45.         if (v[cur].next[x] == 0) {
  46.             return 0;
  47.         } else {
  48.             cur = v[cur].next[x];
  49.         }
  50.     }
  51.     return dp[cur];
  52. }
  53.  
  54. signed main() {
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(nullptr);
  57.     cout.tie(nullptr);
  58.  
  59.     int n;
  60.     cin >> n;
  61.     for (int i = 0; i < n; ++i) {
  62.         string s;
  63.         cin >> s;
  64.         add(s);
  65.     }
  66.  
  67.     int m;
  68.     cin >> m;
  69.     for (int i = 0; i < m; ++i) {
  70.         int type;
  71.         string s;
  72.         cin >> type >> s;
  73.         if (type == 1) {
  74.             add(s);
  75.         } else {
  76.             cout << get(s) << '\n';
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement