【剑指offer】19、正则表达式匹配 && 【Leetcode】44、Wildcard Matching

 题目一

请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

思路

用动态规划来解决

规定

,则str[0~i-1]和pattern[0~j-1]匹配

1、当p[j-1] != * 时

  

2、当p[j-1] = * 时

  记p[j-2]=X,分为以下两种情况

  (a) "X*"重复了0次:等式右边第一项说明*重复零次,则f[i][j]与 f[i][j-2]真假相同,亦或是p[j-2]是万能字符

    

  (b)"X*"重复了大于等于1次:

    等式右边第一项说明要么是万能字符,

    第二项表示f[i][j]与f[i-1][j]真假相同,其实就是回退到重复0次的情况

    第三项保证复制的正确性

      

    如 s=bcaaa p=bca*  f[5][4]  -> f[4][4] -> f[3][4] -> f[2][4]

    此时 s = bc p = bca*,可以匹配

 算法流程

1、初始化二维布尔数组,注意列不一定为0

  

2、按以上规则自下网上计算出f[i][j]

计算过程

  b c a a a
1 0 0 0 0 0
b 0 1 0 0 0 0
c 0 0 1 0 0 0
a 0 0 0 1 0 0
* 0 0 1 1 1 1

 

 

 

 

 

 

 

class Solution {
public:
    bool match(char* str, char* pattern)
    {
        int m = strlen(str);
        int n = strlen(pattern);
        vector<vector<bool>> f(m+1, vector<bool>(n+1, false));
        f[0][0] = true;
        
        for (int i = 1; i <= m; i++)
        {
            f[i][0] = false;
        }
        for (int j = 1; j <= n; j++)
        {
            f[0][j] = ((j > 1) && (pattern[j-1] == '*') && (f[0][j-2]));
        }
        
        for (int i = 1; i <= m; i++){
            for (int j = 1; j <= n; j++){
                if (pattern[j-1] != '*')
                    f[i][j] = f[i - 1][j - 1] && (str[i - 1] == pattern[j - 1] || '.' == pattern[j - 1]);
                else
                    f[i][j] = f[i][j-2] || (pattern[j-2] == '.') || (str[i-1] == pattern[j-2]) && f[i-1][j];
            }
        }
        return f[m][n];
    }
};

题目二

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.

'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

Example 1:

Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

Input:
s = "acdcb"
p = "a*c?b"
Output: false

class Solution {
public:
    bool isMatch(string s, string p) {
        int n = (int)s.length();
        int m = (int)p.length();
        
        vector<vector<bool>> dp(n + 1, vector<bool>(m + 1));
        
        dp[0][0] = true;
        for (int i = 1; i <= m && p[i - 1] == '*'; i++)
            dp[0][i] = true;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (p[j - 1] == '*')
                    dp[i][j] =  dp[i - 1][j] || dp[i][j - 1];
                else
                    dp[i][j] = (p[j - 1] == '?' || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];
            }
        }
        
        return dp[n][m];
    }
};

猜你喜欢

转载自www.cnblogs.com/shiganquan/p/9340833.html
今日推荐