zoukankan      html  css  js  c++  java
  • LeetCode 10 Regular Expression Matching

    题目

    大模拟

    c++

    class Solution {
    public:
        int dp[1005][1005];
        bool ans;
        bool isMatch(string s, string p) {
            
            return judge(s,p,0,0);
        
        }
        bool judge(string s,string p,int i,int j)
    {
        if(dp[i][j]==-1)
            return false;
        if(i>=s.length())
        {
            if(j>=p.length())
            {
                return true;
            }
            else{
                if(j==p.length()-1&&p[j]=='*')
                {
                    return judge(s,p,i,j+1);
                }
                if(j<=p.length()-2&&p[j+1]=='*')
                {
                    return judge(s,p,i,j+2);
                }
                return false;
            }
        }
        if(j>=p.length())
        {
            
            dp[i][j]=-1;
            return false;
            
        }
        if(p[j]=='.')
        {
            ans = judge(s,p,i+1,j+1);
            if(ans==true)
                return true;
            else
            {
                if(j<p.length()-1&&p[j+1]=='*')
                {
                     ans = judge(s,p,i,j+2);
                    if(ans==true)
                        return true;
                    for(int k=i+1;k<=s.length();k++)
                    {
                        ans = judge(s,p,k,j+2);
                        if(ans==true)
                            return true;
                    }
                }
                dp[i][j]=-1;
                return false;
            }
        }
        else if(p[j]=='*')
        {
            ans = judge(s,p,i,j+1);
            if(ans==true)
                return true;
            dp[i][j]=-1;
            return false;
        }
        else{
            if(j<p.length()-1&&p[j+1]=='*')
            {
                if(p[j]==s[i])
                {
                    ans = judge(s,p,i,j+2);
                    if(ans==true)
                        return true;
                    for(int k=i+1;k<=s.length();k++)
                    {
                        if(p[j]==s[k-1])
                        {
                            ans = judge(s,p,k,j+2);
                            if(ans==true)
                                return true;
                        }
                        else
                            break;
                    }
                    dp[i][j]=-1;
                    return false;
                }
                else
                {
                    ans = judge(s,p,i,j+2);
                    if(ans==true)
                        return true;
                    dp[i][j]=-1;
                    return false;
                }
            }
            else
            {
                if(p[j]==s[i])
                {
                    ans=judge(s,p,i+1,j+1);
                    if(ans==true)
                        return true;
                    dp[i][j]=-1;
                    return false;
                }
                else
                {
                    dp[i][j]=-1;
                    return false;
                }
            }
        }
    }
        
       
             
    };
    
  • 相关阅读:
    Activity中Intent的知识
    详解 Android 的 Activity 组件
    Android xml资源文件中@的含义
    为App签名的其他方法
    如何将Android程序打包成apk文件
    创建Android虚拟设备(AVD)
    Android开发环境搭建
    论适应和麻木
    一些有趣的图片
    python 写一个scheme 解释器 (二)——简单求值器内核
  • 原文地址:https://www.cnblogs.com/dacc123/p/11059265.html
Copyright © 2011-2022 走看看