LeetCode Regular Expression Matching 网上一个不错的实现(非递归)

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

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

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

以下是牛人的实现:

class Solution {
public:
    bool isMatch(string s, string p) {
        int i, j;
        int m = s.size();
        int n = p.size();
        const char *s1 = s.c_str();
        const char *p1 = p.c_str();
        /**
         * b[i + 1][j + 1]: if s[0..i] matches p[0..j]
         * if p[j] != ‘*‘
         * b[i + 1][j + 1] = b[i][j] && s[i] == p[j]
         * if p[j] == ‘*‘, denote p[j - 1] with x,
         * then b[i + 1][j + 1] is true iff any of the following is true
         * 1) "x*" repeats 0 time and matches empty: b[i + 1][j -1]
         * 2) "x*" repeats 1 time and matches x: b[i + 1][j]
         * 3) "x*" repeats >= 2 times and matches "x*x": s[i] == x && b[i][j + 1]
         * ‘.‘ matches any single character
         */
        bool b[m + 1][n + 1];
        b[0][0] = true;
        for (i = 0; i < m; i++)
        {
            b[i + 1][0] = false;
        }
        // p[0..j - 2, j - 1, j] matches empty iff p[j] is ‘*‘ and p[0..j - 2] matches empty
        for (j = 0; j < n; j++)
        {
            b[0][j + 1] = j > 0 && ‘*‘ == p1[j] && b[0][j - 1];
        }

        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                if (p[j] != ‘*‘)
                {
                    b[i + 1][j + 1] = b[i][j] && (‘.‘ == p1[j] || s1[i] == p1[j]);
                }
                else
                {
                    b[i + 1][j + 1] = b[i + 1][j - 1] && j > 0 || b[i + 1][j] ||
                                b[i][j + 1] && j > 0 && (‘.‘ == p1[j - 1] || s1[i] == p1[j - 1]);
                }
            }
        }
        return b[m][n];
    }

};

这应该是用动态规划的方法实现的,实在看不懂,哪位大神能给指导点拨下,提供一些相关方法的参考书或者简单的例子吗?(继续思考中)

时间: 2024-08-06 07:58:49

LeetCode Regular Expression Matching 网上一个不错的实现(非递归)的相关文章

leetcode——Regular Expression Matching

题目: 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). The function prototype shoul

LeetCode -- Regular Expression Matching 【算法】

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). The function prototype should be

[LeetCode] Regular Expression Matching(递归)

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). The function prototype should be

[LeetCode] Regular Expression Matching 正则表达式匹配

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). The function prototype should be

[LeetCode] Regular Expression Matching

1. 只要求匹配带'.'和'*' 2. '.'可以当作任何字符来匹配 3. 只有'*'需要特殊处理,'*'可以匹配0个或者多个前面的字符. 注意处理好边界情况. class Solution { public: bool isMatch(string s, string p) { int ans = 0; int n = s.size(); int m = p.size(); vector<vector<int>> visit(n+1, vector<int>(m+1)

LeetCode: Regular Expression Matching 解题报告

Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. SOLUTION 1: 思路: 从后往前遍历罗马数字,如果某个数比前一个数小,则把该数在结果中减掉:反之,则在结果中加上当前这个数: GITHUB 代码: https://github.com/yuzhangcmu/LeetCode_algorithm

LeetCode (10): Regular Expression Matching [HARD]

https://leetcode.com/problems/regular-expression-matching/ [描述] 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 ent

[LeetCode][Python]Regular Expression Matching

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin

[LEETCODE][PYTHON][DP]REGULAR EXPRESSION MATCHING

# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character.'*' Matches zero or more of the preceding element. The matchin