问题1:状态的重复记录问题
class Solution {
public:
vector<vector<bool> >dp;
vector<vector<bool> >visit;
bool dfs(const char*s, const char*p, int slen, int plen){
if(visit[slen][plen]){
return dp[slen][plen];
}
if(slen == 0 && plen == 0){
dp[slen][plen] = true;
visit[slen][plen] = true;
return true;
}
if(slen == 0 && plen != 0){
int iteridx = plen - 1;
while(true){
if(p[iteridx] != '*'){
visit[slen][plen] = true;
dp[slen][plen] = false;
return false;
}
while(iteridx >= 0 && p[iteridx] == '*') --iteridx;
if(iteridx < 0){
visit[slen][plen] = true;
dp[slen][plen] = true;
return true;
}
--iteridx;
if(iteridx < 0){
if(iteridx < 0){
visit[slen][plen] = true;
dp[slen][plen] = true;
return true;
}
}
}
}
if(slen != 0 && plen == 0){
visit[slen][plen] = true;
dp[slen][plen] = false;
return false;
}
if(s[slen-1] == p[plen-1] || p[plen-1] == '.'){
bool tmpb = dfs(s,p,slen-1,plen-1);
if(tmpb){
dp[slen][plen] = true;
visit[slen][plen] = true;
}
}
if(p[plen-1]=='*'){
if(p[plen-2] == '.'){
for(int i = slen; i >= 0; --i){
bool tmpb = dfs(s,p,i,plen-2);
if(tmpb){
dp[slen][plen] = true;
visit[slen][plen] = true;
}
}
}
else if(p[plen-2] == '*'){
bool tmpb = dfs(s, p, slen, plen - 2);
if(tmpb){
dp[slen][plen] = true;
visit[slen][plen] = true;
}
}
else{
int idx = slen;
bool tmpb = dfs(s, p, slen, plen -2);
if(tmpb){
visit[slen][plen] = true;
dp[slen][plen] = true;
}
while(idx >= 1 && s[idx - 1] == p[plen-2]){
tmpb = dfs(s, p, idx - 1, plen - 2);
if(tmpb){
visit[slen][plen] = true;
dp[slen][plen] = true;
}
--idx;
}
}
}
visit[slen][plen] = true;
return dp[slen][plen];
}
bool isMatch(const char *s, const char *p) {
if(s == NULL){
return true;
}
if(p == NULL){
return false;
}
int slen = strlen(s), plen = strlen(p);
dp.resize(slen + 1);
visit.resize(slen + 1);
for(int i = 0; i <= slen; ++i){
dp[i].resize(plen + 1, 0);
visit[i].resize(plen + 1, 0);
}
return dfs(s, p, slen, plen);
}
};