Regular expression Matching DP

Source: Internet
Author: User

This topic, I from the night before (August 6 night) debugging to now (August 8 16:21), too sad, not a good sum up, I am too sorry for myself! This is the topic:

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
dynamic planning, in fact, any algorithm is: core thinking + boundary conditions, in fact, a lot of boundary conditions or in the debugging process added up!! the topic, I refer to a number of online blogs, their own summary as follows: first, a two-tuple array is used to record the match of S and P (s represents the source string, p for the target string), e.g. Res[i][j] to true means s[0 ... i-1] and p[0 ... j-1] have been matched. Suppose the current need to determine whether s[0 ... i] and p[0 ... j] matches, that is, res[i+1][j+1] is true:
    • If p[j]== ' * '
(1) if p[j-1]!= '. ' RES[I+1][J+1] is set to true, only one of the following 3 conditions is required: A. Res[i+1][j] is true (at this point, the character that precedes it is taken only 1 times)
0 i-1 i          
0 Span style= "color: #4dce1d;" >..... j-1 J       *    
B. res[i+1][j-1] is true (at this point, the character that precedes it is not taken once, that is, the two are empty) c. res[i][j+1] is true and s[i]==s[i-1] and s[i-1]==p[j-1]. (This time ' * ' takes one of the preceding characters 2 times). and the C option can continue to go recursively. (2) if p[j-1]== '. ' because '. * ' represents 0 or more '. ', which can match any character, so as long as any one of res[i+1][j-1] or res[i+1][j] is true, the remaining res[i+1][j+1],res [I+2] [J+1]......res[s.length ()][j+1] can all be set to true.
    • If p[j]!= ' * '
when (S[i]==p[j] or p[j]== '. ' ) and Res[i][j] is true, res[i+1][j+1] is set to true.   last return Res[s.lenth ()][p.length ()]Here are my main references, with my own code:
1 classSolution {2  Public:3      BOOLIsMatch (stringSstringp) {4           //constexpr int len1 = Static_cast<const int> (s.length ()), len2 = P.length ();5           //bool Res[len1][len2] = {0}; Here itself originally wanted to use the array directly, but the array subscript is required constant expression, anxious Ah, now also did not solve6           intLen1 = s.length () +1, len2 = p.length () +1;7vector<vector<BOOL>> Res (LEN1, vector<BOOL> (Len2,false));8res[0][0] =true;9            for(inti =1; I < len2;i++)//without these 3 sentences, "AaB", "c*a*b" will not passTen           if(P[i-1] =='*') Oneres[0][i] = res[0][i-2]; A  -            for(intj =0; J < len2-1; J + +) -           { the  -                if(P[j] = ='*') -                { -  +                     if(j>0&&p[j-1] !='.') -                     { +                           for(inti =0; I < len1-1; ++i) A                          if(Res[i +1][j-1] || Res[i +1][J] | | I>0&& S[i-1] = = S[i] && s[i-1] = = P[j-1]&& (Res[i][j +1]|| RES[I][J]))//This place must pay attention to the specific conditions to add the restrictions on the good, do not go to the previous for loop from i=0 to I=1 atRes[i +1][j +1] =true; -                     } -                     Else -                     { -                          inti =0; -                     //For (; i < len1;) in                          //if (!res[i+1][j-1] &&!res[i][j]) -                          //++i; This place actually wrote a dead loop. to                           while(j>0&&i < len1-1&&!res[i +1][j-1] &&!res[i+1][j]) +++i; -                           for(; I < len1-1; ++i) theRes[i +1][j +1] =true; *  $                     }Panax Notoginseng                } -                Else  the                { +                      A                      for(inti =0; I < len1-1;++i) the                     if((s[i] = = P[j] | | p[j] = ='.') &&Res[i][j]) +Res[i +1][j +1] =true; -                } $           } $           returnRES[LEN1-1][len2-1]; -      } -};

Regular expression Matching DP

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.