C++ 代码
class Solution {
public:
bool isMatch(string s, string p){
int slen=s.size(),plen=p.size();
if(slen==0&&plen==0)return true;
if(slen==0&&plen!=2)return false;
s=' '+s;
p=' '+p;
vector<vector<bool>>dp(slen+2,vector<bool>(plen+2));
dp[0][0]=true;
for(int i=1;i<=slen;i++){
dp[i][0]=false;
}
if(p[2]=='*'){
dp[0][2]=true;
//printf("dp[0][2]=true\n");
}
if(p==" .*")return true;
int time=0;
for(int i=1;i<=slen;i++){
for(int j=1;j<=plen;j++){
// if(i==3&&j==5){
// printf("s[3]=%c,p[5]=%c,dp[2][4]=%d,time=%d\n",s[i],p[j],(bool)dp[2][4],time++);
// }
if((s[i]==p[j]||p[j]=='.')&&dp[i-1][j-1]==true){
dp[i][j]=true;
//printf("1:dp[%d][%d]=true,time=%d\n",i,j,time++);
}
if(p[j+1]=='*'){
int t=i;
while(t<=slen&&j<=plen&&(s[i]!=p[j]||p[j]=='.')&&((dp[i][j-1]))){
dp[t++][j+1]=true;
//printf("2:dp[%d][%d]=true,time=%d\n",t-1,j+1,time++);
}
t=i;
while(i<=slen&&j<=plen&&s[t]==p[j]&&dp[i-1][j-1]){
dp[t++][j+1]=true;
//printf("3:dp[%d][%d]=%d,time=%d\n",t-1,j+1,(bool)dp[t-1][j+1],time++);
}
}
}
}
return dp[slen][plen];
}
};
C++ 代码
blablabla