题目:https://leetcode.com/problems/regular-expression-matching/description/
题意:给你字符串s、p,让判断s是否符合p的正则匹配
‘.’ Matches any single character.
‘*’ Matches zero or more of the preceding element.
思路:令dp[i][j]代表s[0…i]和p[0…j]能否匹配,这里“*”难处理,代表0个字符和大于0个的情况
代码:
class Solution {
public:
bool dp[1005][1005];
bool isMatch(string s,string p) {
memset(dp,false,sizeof(dp));
dp[0][0] = true;
for(int i = 0;i <= s.length();i++){
for(int j = 1;j <= p.length();j++){
if(i >= 1 && s[i-1] == p[j-1] || p[j-1] == '.')
dp[i][j] = dp[i][j] || dp[i-1][j-1];
if(p[j-1] == '*'){
if(i >= 1 && (s[i - 1] == p[j - 2] || p[j - 2] == '.'))//>0
dp[i][j] = dp[i][j] || dp[i - 1][j];
dp[i][j] = dp[i][j] || dp[i][j - 2];//0
}
}
}
return dp[s.size()][p.size()];
}
};