leetcode regular expression match

本文探讨了状态重复记录问题,并通过具体的代码实现了解决方案。针对字符串匹配问题,使用了深度优先搜索(DFS)结合备忘录的方法来避免重复计算,有效提升了算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题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){
        //not necessary false
          /*  dp[slen][plen] = false;
            visit[slen][plen] = true;
            return false;*/
            int iteridx = plen - 1;
            while(true){
            //organization of while loop
                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;
                }
               // if(p[iteridx] == "." || p)
               --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] == '.'){//enumerate cases
                for(int i = slen; i >= 0; --i){
                   bool tmpb = dfs(s,p,i,plen-2);
                   if(tmpb){
                      // if(!visit[slen][plen]){
                             dp[slen][plen] = true;
                            visit[slen][plen] = true;
                       //}
                   }
                }
            }
            else if(p[plen-2] == '*'){
                bool tmpb = dfs(s, p, slen, plen - 2);
                if(tmpb){
                    //if(!visit[slen][plen]){
                       dp[slen][plen] = true;
                       visit[slen][plen] = true;
                    //}
                }
            }
            else{//a specific character
                int idx = slen;
                //represent zero p[plen-2]
                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]){ //represent more 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);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值