Advertisement
aayyk

Untitled

Dec 15th, 2019
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <climits>
  9. #include <string>
  10. #include <set>
  11. #include <cmath>
  12. #include <map>
  13. #include <unordered_map>
  14. #include <numeric>
  15. #include <random>
  16. #include <memory>
  17. #include <chrono>
  18. #include <iterator>
  19. #include <functional>
  20. #include <unordered_set>
  21. #include <cassert>
  22. #include <cstring>
  23. #ifdef LOCAL
  24. #include "debug.h"
  25. #else
  26. #define debug(x...)
  27. #endif
  28. /*
  29. #pragma GCC optimize("Ofast")
  30. #pragma GCC optimize("O3")
  31. #pragma GCC optimize("unroll-loops")
  32. */
  33. //#define int ll
  34.  
  35.  
  36.  
  37. using namespace std;
  38. typedef long long ll;
  39. typedef long double ld;
  40. typedef pair < int, int > pii;
  41. typedef pair < ll, ll > pll;
  42. #define sz(x) int((x).size())
  43.  
  44. #ifndef LOCAL
  45. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  46. #else
  47. mt19937 rng(228);
  48. #endif
  49.  
  50. const int N = 2e5 + 7;
  51. const int inf = INT_MAX / 2;
  52. const ll INF = LLONG_MAX / 3;
  53. const int MOD = 1e9 + 7;
  54. const double eps = 1e-6;
  55. const string cars[] = {"🚗", "🚕", "🚙"};
  56.  
  57. struct Trie {
  58.     vector < vector < int > > a = vector < vector < int > > (1, vector < int > (26));
  59.     vector < int > cnt = vector < int > (1);
  60.  
  61.     void add(string& s) {
  62.         int v = 0;
  63.         for (char c : s) {
  64.             int flag = (toupper(c) == c);
  65.             c = tolower(c);
  66.  
  67.             int t = c - 'a';
  68.             if (a[v][t]) {
  69.                 v = a[v][t];
  70.             }
  71.             else {
  72.                 a[v][t] = sz(a);
  73.                 v = sz(a);
  74.                 a.push_back(vector < int > (26));
  75.                 cnt.push_back(0);
  76.             }
  77.  
  78.             cnt[v] += flag;
  79.         }
  80.     }
  81.  
  82.     int query(string& s) {
  83.         int v = 0, res = 0;
  84.         for (char c : s) {
  85.             int t = c - 'a';
  86.             if (a[v][t]) {
  87.                 v = a[v][t];
  88.             }
  89.             else {
  90.                 return 0;
  91.             }
  92.         }
  93.         return cnt[v];
  94.     }
  95. } trie;
  96.  
  97. signed main() {
  98. #ifdef LOCAL
  99.     freopen("input.txt", "r", stdin);
  100.     freopen("output.txt", "w", stdout);
  101. #endif
  102.     cout << fixed << setprecision(4);
  103.     ios::sync_with_stdio(false);
  104.     cin.tie();
  105.     cout.tie();
  106.  
  107.     ll pw[55] = { 1 };
  108.     for (int i = 1; i < 55; i++) {
  109.         pw[i] = pw[i - 1] * 229;
  110.     }
  111.  
  112.     int n;
  113.     cin >> n;
  114.  
  115.     for (int i = 0; i < n; i++) {
  116.         string s;
  117.         cin >> s;
  118.         string t = s;
  119.  
  120.         ll hp = 0, hs = 0;
  121.         for (int i = 0; i < sz(s); i++) {
  122.             hp += (s[i] - 'a' + 1) * pw[i];
  123.             hs = hs * pw[1] + (s[sz(s) - i - 1] - 'a' + 1);
  124.  
  125.             if (hs == hp) {
  126.                 t[i] = toupper(s[i]);
  127.             }
  128.         }
  129.         trie.add(t);
  130.         debug(t);
  131.     }
  132.  
  133.     int m;
  134.     cin >> m;
  135.  
  136.     for (int i = 0; i < m; i++) {
  137.         string s;
  138.         cin >> s;
  139.  
  140.         cout << trie.query(s) << "\n";
  141.     }
  142.  
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement