Implement regular expression matching with support for ‘.‘
and ‘*‘
.
‘.‘ 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") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true
BOOLBacktracking (Char* S,Char* p,intP1,intP2,BOOLPointstar) { while(P[P2]! =' /'){ if(P[P2] = ='*'){ if(Backtracking (s,p,p1-1, p2+1,false))return true;//* stands for 0 times while(s[p1]!=' /'){ if(Backtracking (s,p,p1,p2+1, Pointstar))return true;//* stands for,... Times if(Pointstar | | p[p2-1] = = S[p1]) p1++; Else return false; } if(p[p2+1] ==' /')return true; Else return false; } Pointstar=false; if(S[P1] = =P[P2]) {printf ("s=%d, p =%d\n", S[P1],P[P2]); P1++; P2++; if(P[P2] = ='*'){//If A * occurs while(p[p2+1]==p[p2-1] && p[p2+2]=='*') p2+=2;//neglect following a * } } Else if(P[P2] = ='.' ){ if(S[P1] = =' /'&& p[p2+1] !='*')return false; P1++; P2++; if(P[P2] = ='*'){//If. * Occurs while(p[p2+1]=='.'&& p[p2+2]=='*') p2+=2;//neglect following. *Pointstar =true; } } Else if(p[p2+1] =='*') {P2+=2; } Else return false; } if(S[P1] = =' /')return true; Else return false;}BOOLIsMatch (Char* S,Char*p) {returnBacktracking (S,p,0,0,false);}
Ten. Regular Expression Matching