Advertisement
i_love_rao_khushboo

Wildcard Matching - Recursive

Jul 28th, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define pf push_front
  10. #define ppf pop_front
  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. #define vset(v, n, val) v.clear(); v.resize(n, val)
  17.  
  18. typedef pair<int, int> pii;
  19. typedef pair<ll, ll> pll;
  20. typedef vector<int> vi;
  21. typedef vector<ll> vll;
  22. typedef vector<ull> vull;
  23. typedef vector<bool> vb;
  24. typedef vector<char> vc;
  25. typedef vector<string> vs;
  26. typedef vector<pii> vpii;
  27. typedef vector<pll> vpll;
  28. typedef vector<vi> vvi;
  29. typedef vector<vll> vvll;
  30. typedef vector<vull> vvull;
  31. typedef vector<vb> vvb;
  32. typedef vector<vc> vvc;
  33. typedef vector<vs> vvs;
  34.  
  35. /************************************************** DEBUGGER *******************************************************************************************************/
  36.  
  37. #ifndef ONLINE_JUDGE
  38. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  39. #else
  40. #define debug(x)
  41. #endif
  42.  
  43. void _print(ll t) { cerr << t; }
  44. void _print(int t) { cerr << t; }
  45. void _print(string t) { cerr << t; }
  46. void _print(char t) { cerr << t; }
  47. void _print(ld t) { cerr << t; }
  48. void _print(double t) { cerr << t; }
  49. void _print(ull t) { cerr << t; }
  50.  
  51. template <class T, class V> void _print(pair <T, V> p);
  52. template <class T> void _print(vector <T> v);
  53. template <class T> void _print(vector <vector<T>> v);
  54. template <class T> void _print(set <T> v);
  55. template <class T, class V> void _print(map <T, V> v);
  56. template <class T> void _print(multiset <T> v);
  57. template <class T, class V> void _print(multimap <T, V> v);
  58. template <class T> void _print(queue <T> v);
  59. template <class T> void _print(priority_queue <T> v);
  60. template <class T> void _print(stack <T> s);
  61.  
  62. // modify it's definition below as per need such as it can be used for STL containers with custom args passed
  63. template <class T> void _print(T v);
  64.  
  65. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  66. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  67. 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; } }
  68. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  69. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  70. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  71. template <class T, class V> void _print(multimap <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  72. template <class T> void _print(queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.front()); v.pop(); cerr << " "; } cerr << "]"; }
  73. template <class T> void _print(priority_queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  74. template <class T> void _print(stack <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  75. template <class T> void _print(T v) {  }
  76.  
  77. /*******************************************************************************************************************************************************************/
  78.  
  79. const int INF = 0x3f3f3f3f;
  80. const int mod = 1e9+7;
  81.  
  82. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  83.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  84.                          
  85. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  86. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  87.  
  88. /******************************************************************************************************************************/
  89.  
  90. bool is_matching(string &str, string &pat, int i, int j) {
  91.     // base case(s)
  92.     if((i == -1) and (j == -1)) return 1;
  93.     if((i == -1) or (j == -1)) return 0;
  94.    
  95.     if(isalpha(pat[j])) {
  96.         if(str[i] != pat[j]) return 0;
  97.         return is_matching(str, pat, i - 1, j - 1);
  98.     }
  99.    
  100.     if(pat[j] == '?') {
  101.         return is_matching(str, pat, i - 1, j - 1);
  102.     }
  103.    
  104.     bool ok = is_matching(str, pat, i, j - 1);
  105.    
  106.     for(int k = i - 1; k >= -1; k--) {
  107.         ok |= (is_matching(str, pat, k, j - 1));
  108.     }
  109.    
  110.     return ok;
  111. }
  112.  
  113. void solve()
  114. {
  115.     string str, pat;
  116.     cin >> str >> pat;
  117.    
  118.     // get the length of string and wildcard pattern
  119.     int n = sz(str);
  120.     int m = sz(pat);
  121.    
  122.     if(is_matching(str, pat, n - 1, m - 1)) cout << "YES\n";
  123.     else cout << "NO\n";
  124. }
  125.  
  126. int main()
  127. {
  128.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  129.  
  130.     // #ifndef ONLINE_JUDGE
  131.     //     freopen("input.txt", "r", stdin);
  132.     //     freopen("output.txt", "w", stdout);
  133.     // #endif
  134.    
  135.     // #ifndef ONLINE_JUDGE
  136.     //      freopen("error.txt", "w", stderr);
  137.     // #endif
  138.    
  139.     int t = 1;
  140.     // int test = 1;
  141.     // cin >> t;
  142.     while(t--) {
  143.         // cout << "Case #" << test++ << ": ";
  144.         solve();
  145.     }
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement