Leetcode 10. Regular Expression Matching(递归,dp)

10. Regular Expression Matching
Hard

Given an input string (s) and a pattern (p), 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).

Note:

  • s could be empty and contains only lowercase letters a-z.
  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

题解:这个题如果用暴力的想法很难想清楚,因为对于.*的处理很难,但是如果我们理解.*可匹配也可不匹配这样一个性质就很容易联想递归或者DP的思路了:

这道题分的情况的要复杂一些,先给出递归的解法:

- 若p为空,且s也为空,返回true,反之返回false

- 若p的长度为1,且s长度也为1,且相同或是p为'.'则返回true,反之返回false

- 若p的第二个字符不为*,且此时s为空则返回false,否则判断首字符是否匹配,且从各自的第二个字符开始调用递归函数匹配

- 若p的第二个字符为*,s不为空且字符匹配,调用递归函数匹配s和去掉前两个字符的p,若匹配返回true,否则s去掉首字母

- 返回调用递归函数匹配s和去掉前两个字符的p的结果

由于我对递归结束的判断实在是太恶心了。。。。于是时间和空间都很慢,不过因为是最好理解的思路,所以还是把代码扔上来:

 1 class Solution {
 2 public:
 3     int in(char ch){
 4         if(ch=='.') return 2;
 5         else if(ch=='*') return 3;
 6         else return 1;
 7     }
 8     bool isMatch(string s, string p) {
 9         int lens = s.length();int lenp = p.length();
10         if(lens==0 && lenp==0) { return 1;}
11         if(lens==1 && lenp==1 && s[0]==p[0]) {return 1;}
12         if(lens==0 && lenp==1) return 0;
13         if(lens!=0 && lenp==0) { return 0;}
14         if(lens==0){
15             if((in(p[0])==2&&in(p[1])!=3)||(in(p[0])==2 &&in(p[1])!=3)) return 0;
16             if(in(p[1])==3) return isMatch(s,p.substr(2,lenp));
17         }
18         if(in(p[0])==1&&in(p[1])!=3){
19             if(lens==0||p[0]!=s[0]) { return 0;}
20             else return isMatch(s.substr(1,lens),p.substr(1,lenp));   
21         }
22         if(in(p[0])==2&&in(p[1])!=3){
23             if(lens==0) { return 0;}
24             else return isMatch(s.substr(1,lens),p.substr(1,lenp));   
25         }
26         if(in(p[0])==1&&in(p[1])==3){
27             if(p[0]!=s[0]) return isMatch(s,p.substr(2,lenp));
28             else return max(isMatch(s.substr(1,lens),p),isMatch(s,p.substr(2,lenp)));
29         }
30         if(in(p[0])==2&&in(p[1])==3)
31             return max(isMatch(s.substr(1,lens),p),isMatch(s,p.substr(2,lenp)));
32         cout<<4<<endl; return 1;
33     }
34 };

后来在网上看到了大佬原来可以这么写

 1 class Solution {
 2 public:
 3     bool isMatch(string s, string p) {
 4         if (p.empty()) return s.empty();
 5         if (p.size() > 1 && p[1] == '*') {
 6             return isMatch(s, p.substr(2)) || (!s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p));
 7         } else {
 8             return !s.empty() && (s[0] == p[0] || p[0] == '.') && isMatch(s.substr(1), p.substr(1));
 9         }
10     }
11 };

我们也可以用DP来解,定义一个二维的DP数组,其中dp[i][j]表示s[0,i)和p[0,j)是否match,然后有下面三种情况(下面部分摘自:https://leetcode.com/problems/regular-expression-matching/discuss/5684/9-lines-16ms-c-dp-solutions-with-explanations):

1.  P[i][j] = P[i - 1][j - 1], if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
2.  P[i][j] = P[i][j - 2], if p[j - 1] == '*' and the pattern repeats for 0 times;
3.  P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'), if p[j - 1] == '*' and the pattern repeats for at least 1 times.

 1 class Solution {
 2 public:
 3     bool isMatch(string s, string p) {
 4         int m = s.size(), n = p.size();
 5         vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));
 6         dp[0][0] = true;
 7         for (int i = 0; i <= m; ++i) {
 8             for (int j = 1; j <= n; ++j) {
 9                 if (j > 1 && p[j - 1] == '*') {
10                     dp[i][j] = dp[i][j - 2] || (i > 0 && (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j]);
11                 } else {
12                     dp[i][j] = i > 0 && dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
13                 }
14             }
15         }
16         return dp[m][n];
17     }
18 };
原文地址:https://www.cnblogs.com/shanyr/p/11431099.html