10. Regular Expression Matching(hard)

10. 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:
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

解析

  • 动态规划的问题一直都是难点,需要找出状态转移方程!!!
如果“*”不好判断,那我大不了就来个暴力的算法,把“*”的所有可能性都测试一遍看是否有满足的,用两个指针i,j来表明当前s和p的字符。
我们采用从后往前匹配,为什么这么匹配,因为如果我们从前往后匹配,每个字符我们都得判断是否后面跟着“*”,而且还要考虑越界的问题。但是从后往前没这个问题,一旦遇到“*”,前面必然有个字符。

    如果j遇到”*”,我们判断s[i] 和 p[j-1]是否相同,
        如果相同我们可以先尝试匹配掉s的这个字符,i–,然后看之后能不能满足条件,满足条件,太棒了!我们就结束了,如果中间出现了一个不满足的情况,马上回溯到不匹配这个字符的状态。
        不管相同不相同,都不匹配s的这个字符,j-=2 (跳过“*”前面的字符)

if(p[j-1] == '.' || p[j-1] == s[i])
    if(myMatch(s,i-1,p,j))
        return true;
    return myMatch(s,i,p,j-2);

    如果j遇到的不是“*”,那么我们就直接看s[i]和p[j]是否相等,不相等就说明错了,返回。

    if(p[j] == '.' || p[j] == s[i])
         return myMatch(s,i-1,p,j-1);
    else return false;

    再考虑退出的情况
        如果j已经<0了说明p已经匹配完了,这时候,如果s匹配完了,说明正确,如果s没匹配完,说明错误。
        如果i已经<0了说明s已经匹配完,这时候,s可以没匹配完,只要它还有”*”存在,我们继续执行代码。
// 10. Regular Expression Matching
class Solution_10 {
public:
    /*
    动态规划
    如果 p[j] == str[i] || pattern[j] == '.', 此时dp[i][j] = dp[i-1][j-1];
    如果 p[j] == '*'
    分两种情况:
    1: 如果p[j-1] != str[i] && p[j-1] != '.', 此时dp[i][j] = dp[i][j-2] //*前面字符匹配0次
    2: 如果p[j-1] == str[i] || p[j-1] == '.'
    此时dp[i][j] = dp[i][j-2] // *前面字符匹配0次
    或者 dp[i][j] = dp[i][j-1] // *前面字符匹配1次
    或者 dp[i][j] = dp[i-1][j] // *前面字符匹配多次
    */
    bool isMatch(string s, string p) { //p去匹配s

        vector<vector<bool> > dp(s.size() + 1, vector<bool>(p.size() + 1, false));

        dp[0][0] = true; // 空串匹配空串
        //第一列空串p去匹配,为false
        //第一行非空串p去匹配空串s;只要p中有*,就可以匹配
        for (int i = 1; i < dp[0].size(); i++)
        {
            if (p[i - 1] == '*')
            {
                dp[0][i] = i>1 && dp[0][i - 2];
            }
        }
        for (int i = 1; i <= s.size();i++)
        {
            for (int j = 1; j <= p.size();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]!='.') //匹配*前面的字符0次,跳过当前p
                    {
                        dp[i][j] = dp[i][j-2];
                    }
                    else
                    {
                        //*前面字符匹配1次 || *前面字符匹配0次 || *前面字符匹配多次
                        dp[i][j] = dp[i][j - 1] || dp[i][j - 2] || dp[i - 1][j];
                    }
                }
            }
        }

        return dp[s.size()][p.size()];
    }
};

题目来源

原文地址:https://www.cnblogs.com/ranjiewen/p/8312568.html

时间: 2024-11-01 08:35:36

10. Regular Expression Matching(hard)的相关文章

10. Regular Expression Matching &amp;&amp; 44. Wildcard Matching

10. 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).

(待解决)LeetCode 10. Regular Expression Matching 解题报告

10. Regular Expression Matching 提交网址: https://leetcode.com/problems/regular-expression-matching/ Total Accepted: 79548 Total Submissions: 361279 Difficulty: Hard Implement regular expression matching with support for '.' and '*'. '.' Matches any sing

刷题10. Regular Expression Matching

一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处理有问题,始终无法成功. #include<iostream> using namespace std; class Solution{ public: bool isMatch(string s,string p){ bool result = true; if(s.length()<=0

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】10.Regular Expression Matching

题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. 解题思路: 这道题如果只考虑“.”的话其实很好完成,所以解题的关键在于处理“*”的情况.以为“*”与前一个字母有关,所以应该整体考虑ch*……的情况.ch*可以匹配0-n个s的字符串

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 problem 10 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 10 regular expression matching (正则表达式匹配) (动态规划)

Leetcode 10 问题描述 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 entir