Leetcode Regular Expression Matching Online A good implementation (non-recursive)

Source: Internet
Author: User

‘.‘ 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.