‘?‘ Matches any single character. ' * ' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be:bool IsMatch (const char *s, const char *p) Some examples:ismatch ("AA", "a") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "*") →trueismatch ("AA", "A *") →trueismatch ("AB", "? *") →truei Smatch ("AaB", "C*a*b") →false
1. Dynamic planning
BOOLIsMatch (stringSstringp) {intls = s.length (), LP =p.length (), I, J; Vector<vector<BOOL>> DP (2, vector<BOOL> (lp+1,0)); BOOLK =1; dp[0][0] =1; for(i =1; I <= LP; i++) dp[0][i] = dp[0][i-1] &&'*'= = p[i-1]; for(i =1; I <= LS; i++) {dp[k][0] =0; for(j =1; J <= LP; J + +) { if('*'= = p[j-1]) Dp[k][j]= dp[k][j-1] || dp[!K] [j]; ElseDp[k][j]= dp[!k][j-1] && (p[j-1] = = s[i-1] ||'?'= = p[j-1]); } k= !K; } returndp[!K] [LP];}
2. Do not match when returning to the previous asterisk where the asterisk matches one character more.
BOOLIsMatch (stringSstringp) {intls = s.length (), LP = P.length (), last_i =-1, Last_j =-1, i =0, j =0; while(S[i]) {if('*'==P[j]) {J++; if(!P[j])return 1; Last_i=i; Last_j=J; } Else if(S[i] = = P[j] | |'?'==P[j]) {i++; J++; } Else if(Last_i! =-1) {i= ++last_i; J=Last_j; } Else return 0; } while('*'==P[j]) J++; return!p[j];}
Wildcard Matching *hard*