算法
- 集合:所有
S[1~i
]和P[1~j]
的匹配方案 - 属性:是否存在一个合法方案(bool)
- 如果
p[j] != '*'
,dp[i][j] = dp[i-1][j-1] && (s[i]==p[j] || p[j]=='?')
- 如果
p[j] == '*'
,dp[i][j] = dp[i][j - 1] || dp[i-1][j-1] || dp[i-2][j-1] || dp[i-3][j-1]...
代码
class Solution {
public:
bool isMatch(string s, string p) {
s = ' ' + s, p = ' ' + p;
int n = s.size(), m = p.size();
vector<vector<bool>> dp(n, vector<bool>(m));
dp[0][0] = true;
for(int i = 0; i < n; i++)
for(int j = 1; j < m; j++)
{
char ch = p[j];
if(ch != '*')
{
if(i && (s[i] == ch || ch == '?'))
dp[i][j] = dp[i - 1][j - 1];
}
else
{
dp[i][j] = dp[i][j - 1];
if(i) dp[i][j] = dp[i][j] || dp[i - 1][j];
}
}
return dp[n - 1][m - 1];
}
};
标签:ch,匹配,int,44,通配符,bool,dp,size
From: https://www.cnblogs.com/INnoVationv2/p/16895567.html