标签:als ++ http public etc mat express turn tco
https://leetcode.com/problems/regular-expression-matching/
class Solution {
public:
bool isMatch(string s, string p) {
/*
动态规划问题:
dp[i][j]代表s[0...i-1],和p[0...j-1]是否匹配
p[j-1]=='.'时,dp[i][j]=dp[i-1][j-1]
p[j-1]=='*'时,dp[i][j]=dp[i][j-2] | (dp[i-1][j] & (p[j-2]=='.' | s[i-1]==p[j-2]))对应0字符、有字符
其他情况,dp[i][j]=dp[i-1][j-1] & s[i-1]==p[j-1]
*/
const int Ls=s.size(),Lp=p.size();
bool dp[Ls+1][Lp+1];
dp[0][0]=true;
for(int j=1;j<=Lp;++j){
if(p[j-1]=='*')
dp[0][j]=dp[0][j-2];
else
dp[0][j]=false;
}
for(int i=1;i<=Ls;++i){
dp[i][0]=false;
for(int j=1;j<=Lp;++j){
if(p[j-1]=='.')
dp[i][j]=dp[i-1][j-1];
else if(p[j-1]=='*')
dp[i][j]=dp[i][j-2] | (dp[i-1][j] & (p[j-2]=='.' | s[i-1]==p[j-2]));
else
dp[i][j]=dp[i-1][j-1] & s[i-1]==p[j-1];
}
}
return dp[Ls][Lp];
}
};
Leetcode 10. Regular Expression Matching
标签:als ++ http public etc mat express turn tco
原文地址:https://www.cnblogs.com/ximelon/p/10769688.html