zoukankan      html  css  js  c++  java
  • 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
    @首先题目要理解,通配符*是重复前面一个元素,而不是*前面所有的元素而且通配符*号前面必须要有元素,就是说*出现的位置不可能在第一位。
    f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j];

    f[i][j - 2]表示前面的元素出现0次,后面表示出现次数大于等于1.

    aabbb

    aab.*

    能够出现多次,说明s中减少一个(i -1)也能匹配,所以这个条件也必须满足。

    s[i - 1] == p[j - 2]因为ij表示出现的元素个数,相当于下标从i - 1,j - 1.
    表示p中倒数第二个元素要和s中倒数第一个元素相等。这样才能进行重复。
    注意初始化第一列的情况。
    class Solution {
    public:
        bool isMatch(string s, string p) {
            if(s.size() == 0 && p.size() == 0){
                return true;
            }
            int m = s.size();
            int n = p.size();
            vector<vector<bool>> dp(m + 1,vector<bool> (n + 1,false));
            
            dp[0][0] = true;
            for(int i = 1;i <= m;++i){
                dp[i][0] = false;
            }
            for(int j = 1;j <= n;++j){
                if((j > 1) && (j % 2 == 0) && dp[0][j - 2] && p[j - 1] == '*'){
                    dp[0][j] = true;
                }
            }
            
            for(int i = 1;i <= m;++i){
                for(int j = 1;j<= n;++j){
                    if(p[j - 1] != '*'){
                        dp[i][j] = dp[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
                    }
                    else{
                        dp[i][j] = dp[i][j - 2] || dp[i - 1][j] && ((s[i - 1] == p[j - 2]) || '.' == p[j - 2]);
                    }
                }
            }
            return dp[m][n];
        }
    };
  • 相关阅读:
    【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
    【POJ 2152】 Fire (树形DP)
    【POJ 1741】 Tree (树的点分治)
    【POJ 2486】 Apple Tree (树形DP)
    【HDU 3810】 Magina (01背包,优先队列优化,并查集)
    【SGU 390】Tickets (数位DP)
    【SPOJ 2319】 BIGSEQ
    【SPOJ 1182】 SORTBIT
    【HDU 5456】 Matches Puzzle Game (数位DP)
    【HDU 3652】 B-number (数位DP)
  • 原文地址:https://www.cnblogs.com/dingxiaoqiang/p/7739913.html
Copyright © 2011-2022 走看看