Advertisement
i_love_rao_khushboo

Untitled

Sep 18th, 2022 (edited)
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. // RECURSIVE APPROACH
  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. int cnt_ways(string &s1, string &s2, int n, int m) {
  81.     // base case(s)
  82.     if(n == 0 and m != 0) return 0;
  83.     if(n < m) return 0;
  84.     if(n == 0 or m == 0) return 1;
  85.    
  86.     if(s1[n-1] == s2[m-1]) {
  87.         return cnt_ways(s1, s2, n - 1, m) + cnt_ways(s1, s2, n - 1, m - 1);
  88.     }
  89.    
  90.     else return cnt_ways(s1, s2, n - 1, m);
  91. }
  92.  
  93. void solve()
  94. {
  95.     string s1, s2; cin >> s1 >> s2;
  96.    
  97.     int n = sz(s1);
  98.     int m = sz(s2);
  99.    
  100.     cout << cnt_ways(s1, s2, n, m) << "\n";
  101. }
  102.  
  103. int main()
  104. {
  105.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  106.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  107.  
  108.     // #ifndef ONLINE_JUDGE
  109.     //     freopen("input.txt", "r", stdin);
  110.     //     freopen("output.txt", "w", stdout);
  111.     // #endif
  112.    
  113.     // #ifndef ONLINE_JUDGE
  114.     //      freopen("error.txt", "w", stderr);
  115.     // #endif
  116.    
  117.     int t = 1;
  118.     // int test = 1;
  119.     // cin >> t;
  120.     while(t--) {
  121.         // cout << "Case #" << test++ << ": ";
  122.         solve();
  123.     }
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement