zoukankan      html  css  js  c++  java
  • 正则表达式匹配

    class Solution {
    public:
        bool isMatch(string s, string p) {
            int m=s.size(),n=p.size();
            vector<vector<bool>> dp(m+1,vector<bool>(n+1,false));

            dp[0][0]=true;
            //init row
            for(int j=1;j<n+1;j++){
                if(j>1 && dp[0][j-2]==true && p[j-1]=='*'){
                    dp[0][j]=true;
                }
            }

            //init col

            for(int i=1;i<m+1;i++){
                for(int j=1;j<n+1;j++){
                    if(s[i-1]==p[j-1] || p[j-1]=='.'){
                        dp[i][j]=dp[i-1][j-1];
                    }
                    else if(p[j-1]=='*'){
                        if(s[i-1]!=p[j-2] && p[j-2]!='.'){
                            dp[i][j]=dp[i][j-2];
                        }
                        else{
                            dp[i][j]=dp[i][j-2] || dp[i][j-1] || dp[i-1][j];
                        }
                    }
                }
            }
            return dp[m][n];

        }
    };
  • 相关阅读:
    hdu 1381 Crazy Search
    hdu 5131 Song Jiang's rank list
    poj 2251 Dungeon Master
    hdu 4941 Magical Forest
    hdu 1728 逃离迷宫
    hdu 2612 Find a way
    hdu 3288 Resource Allocation
    hdu 1272 小希的迷宫
    hdu 5224 Tom and paper
    hdu 5104 Primes Problem
  • 原文地址:https://www.cnblogs.com/zijidan/p/12434998.html
Copyright © 2011-2022 走看看