Test instructions
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 (Hard)
Analysis:
The title means a regular expression match, '. ' Matches any character, ' * ' means that the previous character can appear any number of times (0 times, 1 times, 2 times ...). )
such as AB and. *: Will. appears two times for. Can match any character, all can match AB
AaB and c*a*b:c* will C appear 0 times, A * will appear two times, get AaB can match.
and the special symbol is only in the P string (the beginning does not understand this causes the feeling problem is very complicated to push down ..., probably because did not learn the regular expression)
Analytical:
Use dynamic planning. The common state selection for two-sequence dynamic programming is DP[I][J] represents the first I of S and the first J of P ...
Corresponds to the subject,Dp[i][j] Indicates whether the first I character of S and the first J characters of P can match . But the subject of recursive relationship is not very good push complete (after all hard)
When p[j-1]! = ' * '
Need s[i-1] = = P[j-1] and dp[i-1][j-1] = = True (former i-1 and former j-1 can match)
When p[j-1] = = ' * '
There are several situations:
1) * Before the characters need to repeat 0 times such as Match AB and aba*, in which case dp[i][j] is true depending on whether dp[i][j-2] is true;
2) * Before the character needs to repeat 1 times, that is, its own, such as matching ABA and aba*, in which case dp[i][j] is true depending on whether dp[i][j-1] is true;
3) * Previous characters need to be repeated 2 times or more, such as matching Abaa with aba* (appearing two times), matching AAA with. * (appears more than two times);
In this case need s[i-1] = = P[j-2] && (dp[i-1][j-1] | | dp[i-1][j])//dp[i-1][j] easy to ignore, indicating to use the * former element is greater than two times;
Initialize dp[0][0], dp[i][0] (i =,... s.size ()), Dp[0][j], (j =,... p.size ());
where dp[0][j] needs to be judged, p[j-1] = = ' * ' && dp[0][j-2] (that is, * help to remove the preceding characters.) Started also wrote | | Dp[0][j-1], later found that * will not begin to exist, so dp[0][j-1] not necessary)
Code: (There are some small details in the comments)
1 classSolution {2 Public:3 BOOLIsMatch (stringSstringp) {4 BOOLDp[s.size () +1][p.size () +1];5dp[0][0] =true;6 for(inti =1; I <= s.size (); ++i) {7dp[i][0] =false;8 }9 for(intj =1; J <= P.size (); ++j) {Ten //P[j-2] must exist, * will not begin! One if(P[j-1] =='*'&& dp[0][j-2]) { Adp[0][J] =true; - } - Else { thedp[0][J] =false; - } - } - + for(inti =1; I <= s.size (); ++i) { - for(intj =1; J <= P.size (); ++j) { + if(P[j-1] !='*') { ADP[I][J] = (S[i-1] = = P[j-1] || P[j-1] =='.') && Dp[i-1][j-1]; at } - Else { - //repeat two times or more dp[i-1][j] such as: AAA and. * - //* does not begin, so p[j-2] must exist - BOOLB1 = (S[i-1] = = P[j-2] || P[j-2] =='.') && (Dp[i-1][j-1] || Dp[i-1][j]); - BOOLB2 = dp[i][j-1];//Repeat 1 times in BOOLB3 = dp[i][j-2];//Repeat 0 times -DP[I][J] = B1 | | b2 | |B3; to } + } - } the returndp[s.size ()][p.size ()]; * } $};
studied a discussion area and found a recursive The situation is basically the same, with a little bit of refinement.
I do the first thought of similar Abaa and aba*, * Previous characters repeat two times, so write out the dp[i-1][j-1]. (Mark the first half of the red sentence)
After the submission WA found that there is a situation is not considered, that is, AAA and. * That appears more than two times, so added Dp[i-1][j]
And now carefully consider, actually only need a sentence dp[i-1][j] can deal with the situation is greater than or equal to 2 times, so the marked red sentence can be optimized to
BOOL 1 2 2 ' . ') && 1][j];
LeetCode10 Regular Expression Matching