Leetcode 10.正则表达式匹配 动态规划

 

 

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

leetcode原题

Regular Expression Matching

思路:只有当p==\0时,判断s==\0;否则会出现:s=\0,p=c*。

 先判断是否p+1==*,然后进入循环,循环结束return match(str,p+2),不然会出现s=\0,p=.*,进入不了循环的状况。

else if 时要加上str!=\0,不然会出现s=\0,p=.的状况,都+1后s=null。

 1 class Solution {
 2 public:
 3     bool match(char* str, char* pattern)
 4     {
 5         if(*pattern=='\0')
 6             return *str=='\0';
 7         if(*(pattern+1)=='*'){
 8             while(*str!='\0'){
 9                 if(match(str,pattern+2))
10                     return true;
11                 if(*str==*pattern||*pattern=='.')
12                     str++;
13                 else
14                     return false;
15             }
16             return match(str,pattern+2);
17         }
18         else if(*pattern==*str||(*pattern=='.'&&*str!='\0')){
19             return match(str+1,pattern+1);
20         }
21         return false;
22     }
23 };

 思路二:

labuladong

注意首先判断j==p.size()时,return i==s.size

然后判断p[j+1]=='*'的情况

class Solution {
public:
    bool isMatch(string s, string p) {
        int m=s.size();
        int n=p.size();
        return match(s,0,p,0);
    }
    bool match(string s,int i,string p,int j){
        if(j==p.size()) return i==s.size();
        if(j+1<p.size()&&p[j+1]=='*'){
            while(i<s.size()){
                if(match(s,i,p,j+2))
                    return true;
                if(s[i]==p[j]||p[j]=='.')
                    i++;
                else
                    return false;
            }
            return match(s,i,p,j+2);
        }
        else if(i<s.size()&&(s[i]==p[j]||p[j]=='.'))
            return match(s,i+1,p,j+1);
        return false;
    }
};

思路三:

dp数组实现

定义dp[i][j]表示i长度的s与j长度的p是否匹配

则dp[0][0]=true

  1. dp[i][j] = dp[i - 1][j - 1], if p[j - 1] != '*' && (s[i - 1] == p[j - 1] || p[j - 1] == '.');
  2. dp[i][j] = dp[i][j - 2], if p[j - 1] == '*' and the pattern repeats for 0 time;
  3. dp[i][j] = dp[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.'), if p[j - 1] == '*' and the pattern repeats for at least 1 time.

注意,p[j-1]=='*'时,dp[i][j]=dp[i-1][j]&&...

 

class Solution {
public:
    bool isMatch(string s, string p) {
        int m=s.size();
        int n=p.size();
        vector<vector<bool>> dp(m+1,vector<bool>(n+1));
        //s和p都为0,肯定匹配
        dp[0][0]=true;
        //注意i从0开始,因为s=0,p=a* 或者a*b*的时候,dp[0][2]=true dp[0][4]=true
        for(int i=0;i<=m;++i){
            for(int j=1;j<=n;++j){
                //p[j-1]==*
                if(p[j-1]=='*'){
                    //直接略过j-2和j-1 || i>0时 s==p或者p=='.'
                    dp[i][j]=dp[i][j-2]||(i&&dp[i-1][j]&&(s[i-1]==p[j-2]||p[j-2]=='.'));
                }else{
                    dp[i][j]=i&&dp[i-1][j-1]&&(s[i-1]==p[j-1]||p[j-1]=='.');
                }
            }
        }
        return dp[m][n];
    } 
};

 

posted @ 2015-09-09 19:24  鸭子船长  阅读(269)  评论(0编辑  收藏  举报