1. Subject No.
10- regular expression matching (difficult)
2. Method Description
It uses dynamic programming to determine the transfer equation dp [i] [j] =? .
3.10 topics and ideas and code that question
topic:
Give you a character string s and a law p, invite you to implement a support '' and '*' in the regular expression matching.
'.' Matches any single character
'*' matches zero or more of the preceding element that a
so-called matching, to cover the entire string s, and not part of the string.
Description:
s may be empty, and only lowercase letters az from the.
p may be empty and contain only lowercase letters from az, and characters. and *.
Example 1:
Input:
S = "AA"
P = "A"
Output: false
interpretation: "a" can not match the entire string "aa".
Example 2:
Input:
S = "AA"
P = "A *"
Output: true
explanation: because the '*' matches zero or representatives of the foregoing that a plurality of elements, this is in front of the element 'a'. Thus, the string "aa" may be regarded as 'a' is repeated once.
Example 3:
Input:
S = "ab &"
P =. " "
Output: true
interpretation:. " " Represents zero or more matches ( '*') of any character ( '.').
Example 4:
Input:
S = "AAB"
P = "C A B"
Output: true
explanation: because the '*' means zero or more, where 'c' is 0, 'a' is repeated once. So it can match the string "aab".
Example 5:
Input:
S = "Mississippi"
P = "MIS IS P *."
Output: false
Transfer equation solving ideas
Defined dp [i] [j] denotes the front p s i and the first j characters of characters matches
Case 1 , if s [i]p [j], or p [j]'.' (Since '' matches any character, it can be considered equal)
String \ Location | 0 | … | i |
---|---|---|---|
s | … | … | a |
String \ Location | 0 | … | j |
---|---|---|---|
p | … | … | . |
In this case dp [i] [j] = dp [i - 1] [j - 1]
Case 2 , when the p [j] == '*', the following will happen:
!! '.' When 2-1 s [i] = p [j - - 1], and p [j 1] = (* preceding character string does not match the character s)
String \ Location | 0 | … | i |
---|---|---|---|
s | … | … | a |
String \ Location | 0 | … | j - 1 | j |
---|---|---|---|---|
p | … | … | b | * |
Because the '*' matches zero or representatives of the foregoing that a plurality of elements, where only represent 0
In this case dp [i] [j] = dp [i] [j - 2]
2-2 This s [i]p [j - 1], or p [j - 1]'.' ,The following figure:
String \ Location | 0 | … | i |
---|---|---|---|
s | … | … | a |
String \ Location | 0 | … | j - 1 | j |
---|---|---|---|---|
p | … | … | a | * |
Here divided several situations:
2-2-1 when the following pictures, s [i] and p - string matching before [j 2], * Match 0
String \ Location | 0 | … | i - 1 | i |
---|---|---|---|---|
s | … | … | a | b |
String \ Location | 0 | … | j-3 | j - 2 | j-1 | j |
---|---|---|---|---|---|---|
p | … | … | a | b | b | * |
When the image below 2-2-2, s [i - 1] and p - string matching before [j 2], * Match 1
String \ Location | 0 | … | i - 1 | i |
---|---|---|---|---|
s | … | … | a | b |
String \ Location | 0 | … | j - 2 | j-1 | j |
---|---|---|---|---|---|
p | … | … | a | b | * |
When the image below 2-2-3, s [i - 1] and when p [j] in the previous string matching, matching times *
String \ Location | 0 | … | i-2 | i - 1 | i |
---|---|---|---|---|---|
s | … | … | a | b | b |
String \ Location | 0 | … | j - 2 | j-1 | j |
---|---|---|---|---|---|
p | … | … | a | b | * |
Or the three cases of the relationship, the relationship is as follows:
DP [I] [J] DP = [I -. 1] [J] || DP [I -. 1] [J - 2] || DP [I] [ j - 2];
boundary value initialization
1. before performing the above calculation, all values of the array needs to be initialized to false,
2.dp [0] [0] = to true; it represents front before and s 0 p 0 characters a certain character match
3. when calculating i == dp value of 0, a case is also possible to match all match 0 * to:
String \ Location |
---|
s |
String \ Location | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
p | a | * | b | * | c | * |
Code
bool isMatch(char * s, char * p){
int i, j, len1, len2;
len1 = strlen(s);
len2 = strlen(p);
bool** dp;
dp = (bool**)malloc(sizeof(bool*) * (len1 + 1));
for (i = 0; i < len1 + 1; i++) {
dp[i] = (bool*)malloc(sizeof(bool) * (len2 + 1));
memset(dp[i], 0, sizeof(bool) * (len2 + 1));
}
dp[0][0] = true;
for (i = 0; i < len2; i++) {
if (p[i] == '*' && i > 0 && dp[0][i - 1]) {
dp[0][i + 1] = true;
}
}
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
if (s[i] == p[j] || p[j] == '.') {
dp[i + 1][j + 1] = dp[i][j];
continue;
}
if (p[j] == '*' && j > 0) {
if (s[i] == p[j - 1] || p[j - 1] == '.') {
dp[i + 1][j + 1] = dp[i][j + 1] || dp[i][j - 1] || dp[i + 1][j - 1];
} else {
dp[i + 1][j + 1] = dp[i + 1][j - 1];
}
}
}
}
return dp[len1][len2];
}
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/regular-expression-matching
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.