链接:https://leetcode.com/problems/wildcard-matching/description/
题目说明:外卡匹配,使用动态规划的思路。外卡匹配和正则匹配的最大不同就是外卡匹配中星号可以单独存在。
创建匹配数组(用vector
动态规划的初始状态match[0][0]=true;match[i][0]=false;math[0][j]=match[0][j-1] while p[j]='*'
AC代码:
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length();
int n = p.length();
vector> match(m+1 ,vector(n+1, false));
match[0][0] = true;
for (int pi = 1; pi <= n; ++pi)
{
if (p[pi-1] == '*')
{
match[0][pi] = match[0][pi-1];
}
}
for (int si = 1; si <= m; ++si)
{
for (int pi = 1; pi <= n; ++pi)
{
if (s[si-1] == p[pi-1] || p[pi-1] == '?')
{
match[si][pi] = match[si-1][pi-1];
}
else if (p[pi-1] == '*')
{
match[si][pi] = match[si-1][pi] || match[si][pi-1];
}
}
}
return match[m][n];
}
};