码迷,mamicode.com
首页 > 其他好文 > 详细

10. Regular Expression Matching

时间:2020-03-30 12:48:40      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:variable   情况   match   除了   char   mode   lock   end   pattern   

题目描述

Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.‘ and ‘*‘.

‘.‘ Matches any single character.
‘*‘ Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

Note:

  • s could be empty and contains only lowercase letters a-z.

  • p could be empty and contains only lowercase letters a-z, and characters like . or *.

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "a*"
Output: true
Explanation: ‘*‘ means zero or more of the preceding element, ‘a‘. Therefore, by repeating ‘a‘ once, it becomes "aa".

Example 3:

Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".

Example 4:

Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".

Example 5:

Input:
s = "mississippi"
p = "mis*is*p*."
Output: false

难度系数

Hard

解法:动态规划

其中dp[i] [j]表示s[0,i)和p[0,j)是否match,注意左闭右开,状态转移方程如下

1.  P[i][j] = P[i - 1][j - 1], if p[j - 1] != ‘*‘ && (s[i - 1] == p[j - 1] || p[j - 1] == ‘.‘);p和s当前位置字符相等或者p当前位置是‘.‘
2.  P[i][j] = P[i][j - 2], if p[j - 1] == ‘*‘ 匹配0次,匹配0次:s=ab,p=aba*,将p的最后一个a去掉。
3.  P[i][j] = P[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == ‘.‘), if p[j - 1] == ‘*‘ 匹配至少一次

具体代码如下

class Solution {
public:
    bool isMatch(string s, string p){
        int m = s.length(),n = p.length();
        //其中dp[i][j]表示s[0,i)和p[0,j)是否match
        bool dp[m+1][n+1];
?
        dp[0][0] = true;
        //初始化第0行,除了[0][0]全为false,毋庸置疑,因为空串p只能匹配空串,其他都无能匹配
        for (int i = 1; i <= m; i++)
            dp[i][0] = false;
        //初始化第0列,只有X*能匹配空串,如果有*,它的真值一定和p[0][j-2]的相同(略过它之前的符号)
        for (int j = 1; j <= n; j++)
            dp[0][j] = j > 1 && * == p[j - 1] && dp[0][j - 2];
?
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= n; j++){
                if (p[j - 1] == *){
                    //dp[i][j - 2] 是匹配0次的情况,例子:s=ab,p=aba*
                    //dp[i][j-1] 不匹配 例子:s=aba,p=aba*
                    //s[i - 1] == p[j - 2] && dp[i - 1][j]是匹配至少一次,例子:s=abb,p=ab*
                    //p[j - 2] == ‘.‘&& dp[i - 1][j]是匹配至少一次, 例子:s=abb,p=a.*     
                    dp[i][j] = dp[i][j - 2] || dp[i][j-1]||((s[i - 1] == p[j - 2] || p[j - 2] == .) && dp[i - 1][j]);
                }
                else{   
                    //p[j - 1] == ‘.‘ && dp[i - 1][j - 1] 例子:s=abb p=ab.
                    //s[i - 1] == p[j - 1] && dp[i - 1][j - 1]例子:s=abb p=abb
                    dp[i][j] = (p[j - 1] == . || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];
                }
            }
        }
        return dp[m][n];
    }
};

 

 

 

10. Regular Expression Matching

标签:variable   情况   match   除了   char   mode   lock   end   pattern   

原文地址:https://www.cnblogs.com/AntonioSu/p/12597721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!