Advertisement
i_love_rao_khushboo

Untitled

Sep 18th, 2022
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. // MEMOIZED IMPLEMENTATION
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. #define ll long long
  7. #define ld long double
  8. #define ull unsigned long long
  9. #define pb push_back
  10. #define ppb pop_back
  11. #define mp make_pair
  12. #define F first
  13. #define S second
  14. #define PI 3.1415926535897932384626
  15. #define sz(x) ((int)(x).size())
  16.  
  17. typedef pair<int, int> pii;
  18. typedef pair<ll, ll> pll;
  19. typedef vector<int> vi;
  20. typedef vector<ll> vll;
  21. typedef vector<ull> vull;
  22. typedef vector<bool> vb;
  23. typedef vector<char> vc;
  24. typedef vector<pii> vpii;
  25. typedef vector<pll> vpll;
  26. typedef vector<vi> vvi;
  27. typedef vector<vll> vvll;
  28. typedef vector<vull> vvull;
  29. typedef vector<vb> vvb;
  30. typedef vector<vc> vvc;
  31.  
  32. /************************************************** DEBUGGER *******************************************************************************************************/
  33.  
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  36. #else
  37. #define debug(x)
  38. #endif
  39.  
  40. void _print(ll t) { cerr << t; }
  41. void _print(int t) { cerr << t; }
  42. void _print(string t) { cerr << t; }
  43. void _print(char t) { cerr << t; }
  44. void _print(ld t) { cerr << t; }
  45. void _print(double t) { cerr << t; }
  46. void _print(ull t) { cerr << t; }
  47.  
  48. template <class T, class V> void _print(pair <T, V> p);
  49. template <class T> void _print(vector <T> v);
  50. template <class T> void _print(vector <vector<T>> v);
  51. template <class T> void _print(set <T> v);
  52. template <class T, class V> void _print(map <T, V> v);
  53. template <class T> void _print(multiset <T> v);
  54. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  55. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  57. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60.  
  61. /*******************************************************************************************************************************************************************/
  62.  
  63. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  64. int rng(int lim) {
  65.     uniform_int_distribution<int> uid(0,lim-1);
  66.     return uid(rang);
  67. }
  68.  
  69. const int INF = 0x3f3f3f3f;
  70. const int mod = 1e9+7;
  71.  
  72. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  73.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  74.                          
  75. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  76. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  77.  
  78. /******************************************************************************************************************************/
  79.  
  80. vvi dp;
  81.  
  82. int cnt_ways(string &s1, string &s2, int n, int m) {
  83.     // base case(s)
  84.     if(n == 0 and m != 0) return 0;
  85.     if(n < m) return 0;
  86.     if(n == 0 or m == 0) return 1;
  87.    
  88.     // check if already calculated or not
  89.     if(dp[n][m] != -1) return dp[n][m];
  90.    
  91.     if(s1[n-1] == s2[m-1]) {
  92.         return dp[n][m] = cnt_ways(s1, s2, n - 1, m) + cnt_ways(s1, s2, n - 1, m - 1);
  93.     }
  94.    
  95.     else return dp[n][m] = cnt_ways(s1, s2, n - 1, m);
  96. }
  97.  
  98. void solve()
  99. {
  100.     string s1, s2; cin >> s1 >> s2;
  101.    
  102.     int n = sz(s1);
  103.     int m = sz(s2);
  104.    
  105.     dp.clear();
  106.     dp.resize(n + 1, vi(m + 1, -1));
  107.    
  108.     cout << cnt_ways(s1, s2, n, m) << "\n";
  109. }
  110.  
  111. int main()
  112. {
  113.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  114.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  115.  
  116.     // #ifndef ONLINE_JUDGE
  117.     //     freopen("input.txt", "r", stdin);
  118.     //     freopen("output.txt", "w", stdout);
  119.     // #endif
  120.    
  121.     // #ifndef ONLINE_JUDGE
  122.     //      freopen("error.txt", "w", stderr);
  123.     // #endif
  124.    
  125.     int t = 1;
  126.     // int test = 1;
  127.     // cin >> t;
  128.     while(t--) {
  129.         // cout << "Case #" << test++ << ": ";
  130.         solve();
  131.     }
  132.  
  133.     return 0;
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement