【剑指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]

 

计算过程

 bcaaa
100000
b010000
c001000
a000100
*001111

 

 

 

 

 

 

 

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];
    }
};

 

转载于:https://www.cnblogs.com/shiganquan/p/9340833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值