forked from sbmxc/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWildcard_string_matching.cpp
More file actions
45 lines (41 loc) · 962 Bytes
/
Wildcard_string_matching.cpp
File metadata and controls
45 lines (41 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int wildcard(string p, string s, int i, int j){
if(i==-1 and j==-1)
return 1;
if(j==-1){
for(int k=0;k<i;k++){
if(p[k]!='*')
return 0;
}
return 1;
}
if(i==-1)
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
if(p[i]==s[j] or p[i]=='?')
return dp[i][j] = wildcard(p,s,i-1,j-1);
if(p[i]=='*'){
int res1 = wildcard(p,s,i-1,j);
int res2 = wildcard(p,s,i,j-1);
return dp[i][j] = res1 or res2;
}
return dp[i][j] = 0;
}
int main(){
int t;
cin>>t;
while(t--){
string p,s;
cin>>p>>s;
memset(dp,-1,sizeof(dp));
int ans = wildcard(p,s,p.length()-1,s.length()-1);
if(ans==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}