Sword refers to Offer 19. Regular expression matching (C++) dynamic programming

Please implement a function to match regular expressions containing'. 'and' '. The character'.' in the pattern means any character, and ' ' means that the character before it can appear any number of times (including 0 times). In this question, matching means that all characters of the string match the entire pattern. For example, the string "aaa" matches the patterns "aa" and "ab ac a", but does not match neither "aa.a" nor "ab*a".

Example 1:

输入:
s = "aa"
p = "a"
输出: false
解释: "a" 无法匹配 "aa" 整个字符串。

Example 2:

输入:
s = "aa"
p = "a*"
输出: true
解释: 因为 '*' 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是 'a'。因此,字符串 "aa" 可被视为 'a' 重复了一次。

Example 3:

输入:
s = "ab"
p = ".*"
输出: true
解释: ".*" 表示可匹配零个或多个('*')任意字符('.')。

Example 4:

输入:
s = "aab"
p = "c*a*b"
输出: true
解释: 因为 '*' 表示零个或多个,这里 'c' 为 0 个, 'a' 被重复一次。因此可以匹配字符串 "aab"

Example 5:

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

s may be empty and only contain lowercase letters from az.
p may be empty, and only contain lowercase letters and characters from az. And , there is no continuous' '.

Note: This question is the same as the 10 questions on the main site: https://leetcode-cn.com/problems/regular-expression-matching/

Dynamic planning ideas

Detailed reference: https://blog.csdn.net/qq_30457077/article/details/114171933

class Solution {
    
    
public:
    bool isMatch(string s, string p) {
    
    
    //求两个字符串长度
        int m = s.size();
        int n = p.size();
//求是否匹配
        auto matches = [&](int i, int j) {
    
    
        //Int i ;int j中的i、j是指索引
        //当i为1;j不为1的时候,返回false
            if (i == 0) {
    
    
                return false;
            }
            if (p[j - 1] == '.') {
    
    
                return true;
            }
            //返回末尾的前一位;若ture;则
            return s[i - 1] == p[j - 1];
        };

        vector<vector<int>> f(m + 1, vector<int>(n + 1));
        f[0][0] = true;
        for (int i = 0; i <= m; ++i) {
    
    
            for (int j = 1; j <= n; ++j) {
    
    
                if (p[j - 1] == '*') {
    
    
                //p[j-2]匹配0个的时候,f[i][j]为f[i][j - 2]  s[i-1]、p[j-3]判断结果
                    f[i][j] |= f[i][j - 2];//|= 使用的原因:存在true就返回true;全部false才false
                    if (matches(i, j - 1)) {
    
    //当s[i-1]==p[j-2]满足时,可以丢掉s[i-1]继续判断;
                    //f[i][j] 为f[i - 1][j]的判断结果
                    //|=是按位或并赋值的意思。若有两个整型变量a和b,那么a|=b;就是a=a|b;的意思。
                        f[i][j] |= f[i - 1][j];
                    }
                }
                else {
    
    
                //s[i - 1] == p[j - 1]时;
                //f[i][j]为f[i - 1][j - 1]判断结果
                    if (matches(i, j)) {
    
    
                        f[i][j] |= f[i - 1][j - 1];
                    }
                }
            }
        }
        return f[m][n];
    }
};

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114981581