zoukankan      html  css  js  c++  java
  • 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()];
    	}
    };
    
    

    题目来源

  • 相关阅读:
    IntegrityError duplicate key value violates unique constraint
    LeetCode 212: Word Search II
    LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
    LeetCode: Unique Paths II 解题报告
    LeetCode: Unique Paths 解题报告
    LeetCode: Remove Nth Node From End of List 解题报告
    LeetCode: Convert Sorted List to Binary Search Tree 解题报告
    LeetCode: Path Sum II 解题报告
    lintcode: k Sum 解题报告
    LeetCode: Unique Binary Search Trees II 解题报告
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/8312568.html
Copyright © 2011-2022 走看看