Advertisement
aayyk

Untitled

Dec 12th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 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 = 3e4 + 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. vector < int > solve1(vector < string >& t, vector < string >& s) {
  58.     vector < int > res(sz(s));
  59.     for (int i = 0; i < sz(s); i++) {
  60.         for (int j = 0; j < sz(t); j++) {
  61.             if (t[j].substr(0, sz(s[i])) == s[i] && t[j].substr(sz(t[j]) - sz(s[i])) == s[i]) {
  62.                 res[i]++;
  63.             }
  64.         }
  65.     }
  66.  
  67.     return res;
  68. }
  69.  
  70.  
  71. int n, m;
  72.  
  73. struct Tree {
  74.     vector < ll > t[4 * N];
  75.  
  76.     Tree() {}
  77.  
  78.     void init(vector < ll >& a) {
  79.         for (auto& x : t) {
  80.             x.clear();
  81.         }
  82.  
  83.         build(1, 0, sz(a) - 1, a);
  84.     }
  85.  
  86.     void build(int v, int tl, int tr, vector < ll >& a) {
  87.         if (tl == tr) {
  88.             t[v].push_back(a[tl]);
  89.         }
  90.         else {
  91.             int tm = (tl + tr) / 2;
  92.  
  93.             build(2 * v, tl, tm, a);
  94.             build(2 * v + 1, tm + 1, tr, a);
  95.  
  96.             merge(t[2 * v].begin(), t[2 * v].end(),
  97.             t[2 * v + 1].begin(), t[2 * v + 1].end(), back_inserter(t[v]));
  98.         }
  99.     }
  100.  
  101.     int query(int v, int tl, int tr, int l, int r, ll val) {
  102.         if (l > r) {
  103.             return 0;
  104.         }
  105.  
  106.         if (tl == l && tr == r) {
  107.             auto [L, R] = equal_range(t[v].begin(), t[v].end(), val);
  108.             return R - L;
  109.         }
  110.         else {
  111.             int tm = (tl + tr) / 2;
  112.  
  113.             return query(2 * v, tl, tm, l, min(r, tm), val) +
  114.             query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r, val);
  115.         }
  116.     }
  117. } tree[52];
  118.  
  119. void binsearch(vector < string >& t, string& s, int& l, int& r) {
  120.     int L = 0, R = sz(t), M;
  121.     while (R - L > 1) {
  122.         M = (L + R) / 2;
  123.  
  124.         string a = t[M].substr(0, sz(s));
  125.         if (s <= a) {
  126.             R = M;
  127.         }
  128.         else {
  129.             L = M;
  130.         }
  131.     }
  132.  
  133.     if (s < t[L].substr(0, sz(s))) {
  134.         l = 1, r = 0;
  135.         return;
  136.     }
  137.     l = (s <= t[L].substr(0, sz(s)) ? L : R);
  138.  
  139.     L = 0, R = sz(t);
  140.     while (R - L > 1) {
  141.         M = (L + R) / 2;
  142.  
  143.         string a = t[M].substr(0, sz(s));
  144.         if (s >= a) {
  145.             L = M;
  146.         }
  147.         else {
  148.             R = M;
  149.         }
  150.     }
  151.     r = (R != sz(t) && s >= t[R].substr(0, sz(s)) ? R : L);
  152. }
  153.  
  154. vector < int > solve2(vector < string > t, vector < string > s) {
  155.     sort(t.begin(), t.end());
  156.     //debug(t);
  157.  
  158.     vector < vector < ll > > h(sz(t));
  159.  
  160.     for (int j = 0; j < sz(t); j++) {
  161.         h[j].resize(sz(t[j]));
  162.         h[j].back() = t[j].back() - 'a' + 1;
  163.  
  164.         for (int i = sz(t[j]) - 2; i >= 0; i--) {
  165.             h[j][i] = h[j][i + 1] * 229 + (t[j][i] - 'a' + 1);
  166.         }
  167.     }
  168.  
  169.     for (int j = 1; j <= 50; j++) {
  170.         vector < ll > a(sz(t));
  171.         for (int i = 0; i < sz(t); i++) {
  172.             if (sz(t[i]) - j >= 0) {
  173.                 a[i] = (h[i][sz(t[i]) - j]);
  174.             }
  175.         }
  176.  
  177.         tree[j].init(a);
  178.     }
  179.  
  180.     vector < int > res(sz(s));
  181.     for (int i = 0; i < sz(s); i++) {
  182.         int l, r;
  183.         binsearch(t, s[i], l, r);
  184.         //debug(l, r);
  185.  
  186.         if (l > r) {
  187.             continue;
  188.         }
  189.  
  190.         ll h = s[i].back() - 'a' + 1;
  191.         for (int j = sz(s[i]) - 2; j >= 0; j--) {
  192.             h = h * 229 + s[i][j] - 'a' + 1;
  193.         }
  194.  
  195.         res[i] = tree[sz(s[i])].query(1, 0, sz(t) - 1, l, r, h);
  196.     }
  197.  
  198.     return res;
  199. }
  200.  
  201. int rng_on(int l, int r) {
  202.     return rng() % (r - l + 1) + l;
  203. }
  204.  
  205. signed main() {
  206. #ifdef LOCAL
  207.     freopen("input.txt", "r", stdin);
  208.     freopen("output.txt", "w", stdout);
  209. #endif
  210.     cout << fixed << setprecision(4);
  211.     ios::sync_with_stdio(false);
  212.     cin.tie();
  213.     cout.tie();
  214. ///*
  215.     cin >> n;
  216.     vector < string > t(n);
  217.  
  218.     for (string& x : t) {
  219.         cin >> x;
  220.     }
  221.  
  222.     cin >> m;
  223.     vector < string > s(m);
  224.  
  225.     for (string& x : s) {
  226.         cin >> x;
  227.     }
  228.  
  229.     auto ans = solve2(t, s);
  230.     for (int x : ans) {
  231.         cout << x << "\n";
  232.     }
  233.     return 0;
  234. //*/
  235.     for (int it = 0; ; it++) {
  236.         int n = rng_on(1, 10);
  237.         int m = rng_on(1, 1);
  238.  
  239.         vector < string > t(n), s(m);
  240.         for (string& x : t) {
  241.             int len = rng_on(1, 10);
  242.             for (int i = 0; i < len; i++) {
  243.                 x.push_back('a' + rng_on(0, 4));
  244.             }
  245.         }
  246.  
  247.         for (string& x : s) {
  248.             int len = rng_on(1, 10);
  249.             for (int i = 0; i < len; i++) {
  250.                 x.push_back('a' + rng_on(0, 4));
  251.             }
  252.         }
  253.  
  254.         auto s1 = solve1(t, s);
  255.         auto s2 = solve2(t, s);
  256.  
  257.         if (s1 != s2) {
  258.             debug(it);
  259.  
  260.             debug(t);
  261.             debug(s);
  262.  
  263.             debug(s1);
  264.             debug(s2);
  265.             return 0;
  266.         }
  267.     }
  268.  
  269.     return 0;
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement