mr_dot_convict

808G-Anthem-of-berland-codeforces-mr.convict

Mar 29th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.35 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define X         first
  24. #define Y         second
  25. #define sz(x)     (int)x.size()
  26. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  27. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  28. #define cnt(x)    __builtin_popcount(x)
  29. #define cntll(x)  __builtin_popcountll(x)
  30. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  31. #define un(x)     sort(x.begin(), x.end()), \
  32.   x.erase(unique(x.begin(), x.end()), x.end())
  33. using   ll  =     long long;
  34. using   ull =     unsigned long long;
  35. using   ff  =     long double;
  36. using   pii =     pair<int,int>;
  37. using   pil =     pair<int,ll>;
  38. using   pll =     pair<ll,ll>;
  39. using   vi  =     vector <int>;
  40. using   vl  =     vector<ll>;
  41. using   vvi =     vector <vi>;
  42. using   vvl =     vector <vl>;
  43. using   vp  =     vector <pii>;
  44. using   vvp =     vector <vp>;
  45. typedef tree
  46. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  47. ordered_set;
  48.  
  49. struct chash {
  50.   int operator () (pii x) const { return x.X*31 + x.Y; }
  51. };
  52. gp_hash_table <pii, int, chash> gmp;
  53.  
  54. #if __cplusplus > 201103L
  55. seed_seq seq{
  56.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  57.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  58.     (uint64_t) __builtin_ia32_rdtsc(),
  59.     (uint64_t) (uintptr_t) make_unique<char>().get()
  60. };
  61. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  62. #else
  63. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  64. mt19937 rng(seed);
  65. #endif
  66.  
  67. #define debug(args...) { \
  68.   /* WARNING : do NOT compile this debug func calls with following flags: // \
  69.    * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  70.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  71.   stringstream _ss(_s); \
  72.   istream_iterator<string> _it(_ss); err(_it, args); \
  73. }
  74. void err(istream_iterator<string> it) {
  75.   it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  76. }
  77. template<typename T, typename... Args>
  78. void err(istream_iterator<string> it, T a, Args... args) {
  79.   cerr << fixed << setprecision(15)
  80.     << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  81.   err(++it, args...);
  82. }
  83. //         __                                           __
  84. //        (**)                                         (**)
  85. //        IIII                                         IIII
  86. //        ####                                         ####
  87. //        HHHH     Madness comes, and madness goes     HHHH
  88. //        HHHH    An insane place, with insane moves   HHHH
  89. //        ####   Battles without, for battles within   ####
  90. //     ___IIII___        Where evil lives,          ___IIII___
  91. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  92. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  93. // `     }    {     '  just breaking them in    `     }    {     '
  94. //       ) () (  Quickest way out, quickest way wins  ) () (
  95. //       ( :: )      Never disclose, never betray     ( :: )
  96. //       | :: |   Cease to speak or cease to breath   | :: |
  97. //       | )( |        And when you kill a man,       | )( |
  98. //       | || |          you're a murderer            | || |
  99. //       | || |             Kill many                 | || |
  100. //       | || |        and you're a conqueror         | || |
  101. //       | || |        Kill them all ... Ooh..        | || |
  102. //       | || |           Oh you're a God!            | || |
  103. //       ( () )                       -Megadeth       ( () )
  104. //        \  /                                         \  /
  105. //         \/                                           \/
  106. /*}}}*/
  107. /*****************************************************************************/
  108. //Don’t practice until you get it right. Practice until you can’t get it wrong
  109.  
  110. vector <int> pref_func (string &s) { // O(|s|)
  111.   int n = (int)s.size();
  112.   vector <int> pi(n);
  113.    pi[0] = 0;
  114.    int sz = (int)s.size();
  115.    for (int i = 1; i < sz; ++i) {
  116.       int len_j = pi[i-1];
  117.       int j = len_j - 1;
  118.       while (len_j > 0 && s[j + 1] != s[i]) {
  119.          len_j = pi[j];
  120.          j = len_j - 1;
  121.       }
  122.       if (s[len_j] == s[i])
  123.          ++len_j;
  124.       pi[i] = len_j;
  125.    }
  126.    return pi;
  127. }
  128.  
  129. // each state represents prefix length
  130. // transition : length * char -> length
  131. vector <vector <int>> calc_fsm(string t) { // O(|s| * 26)
  132.   int n = (int)t.size();
  133.   t += '#';
  134.   vector <vector <int>> fsm(n + 1, vector <int> (26));
  135.   vector <int> pi = pref_func(t);
  136.   for (int len = 0; len <= n; ++len) {
  137.     for (int ch = 0; ch < 26; ++ch) {
  138.       if (len == 0 || t[len] == char('a' + ch))
  139.         fsm[len][ch] = len + (t[len] == char('a' + ch));
  140.       else
  141.         fsm[len][ch] = fsm[pi[len - 1]][ch];
  142.     }
  143.   }
  144.   return fsm;
  145. }
  146.  
  147. signed main() {
  148.   IOS; PREC;
  149.   string s, t;
  150.   cin >> s >> t;
  151.   vector<vector<int>> fsm = calc_fsm(t);
  152.  
  153.   int n = (int)s.size(), m = (int)t.size();
  154.   int dp[m + 1][2];
  155.   memset(dp, -1, sizeof(dp));
  156.  
  157.   dp[0][0] = 0;
  158.  
  159.   int nxt;
  160.   for (int i = 0; i < n; ++i) {
  161.     for (int j = 0; j <= m; ++j) if (dp[j][i & 1] != -1) {
  162.       if (s[i] == '?') {
  163.         for (int ch = 0; ch < 26; ++ch) {
  164.           nxt = fsm[j][ch];
  165.           dp[nxt][!(i & 1)] = max(dp[nxt][!(i & 1)], dp[j][i & 1] + (nxt == m));
  166.         }
  167.       }
  168.       else {
  169.         nxt = fsm[j][s[i] - 'a'];
  170.         dp[nxt][!(i & 1)] = max(dp[nxt][!(i & 1)], dp[j][i & 1] + (nxt == m));
  171.       }
  172.     }
  173.   }
  174.  
  175.   int res = 0;
  176.   for (int i = 0; i <= m; ++i) res = max(res, dp[i][n & 1]);
  177.   cout << res << '\n';
  178.  
  179.   return EXIT_SUCCESS;
  180. }
Add Comment
Please, Sign In to add comment