‘.‘ Matches any single character.
' * ' Matches zero or more of the preceding element.
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") →false
IsMatch ("AA", "AA") →true
IsMatch ("AAA", "AA") →false
IsMatch ("AA", "A *") →true
IsMatch ("AA", ". *") →true
IsMatch ("AB", ". *") →true
IsMatch ("AaB", "C*a*b") →true
The following is the realization of the Ox man:
classSolution { Public: BOOLIsMatch (stringSstringp) {intI, J; intm =s.size (); intn =p.size (); Const Char*S1 =S.c_str (); Const Char*P1 =P.c_str (); /** * b[i + 1][j + 1]: if S[0..I] matches P[0..J] * if P[J]! = ' * ' * b[i + 1][j + 1] = B[i][j] & amp;& S[i] = = p[j] * if p[j] = = ' * ', denote p[j-1] with x, * then b[i + 1][j + 1] are true iff any of The following is true * 1) "x*" repeats 0 time and matches Empty:b[i + 1][j-1] * 2) "x*" Repeats 1 time and matches X:b[i + 1][j] * 3) "x*" repeats >= 2 times and matches "x*x": s[i] = = x && b[i][j + 1] * '. ' matches any single character*/ BOOLB[m +1][n +1]; b[0][0] =true; for(i =0; I < m; i++) {B[i+1][0] =false; } //P[0..j-2, J-1, J] matches empty iff p[j] is ' * ' and p[0..j-2] matches empty for(j =0; J < N; J + +) {b[0][j +1] = j >0&&'*'= = P1[j] && b[0][j-1]; } for(i =0; I < m; i++) { for(j =0; J < N; J + +) { if(P[j]! ='*') {B[i+1][j +1] = B[i][j] && ('.'= = P1[j] | | S1[i] = =P1[j]); } Else{b[i+1][j +1] = B[i +1][j-1] && J >0|| B[i +1][J] | |b[i][j+1] && J >0&& ('.'= = P1[j-1] || S1[i] = = P1[j-1]); } } } returnB[m][n]; }};
This should be the method of dynamic planning to achieve, really do not understand, which God can give guidance to the guide, provide some relevant methods of reference books or simple examples? (continue to think)
Leetcode Regular Expression Matching Online A good implementation (non-recursive)