【题意】

给两个字符串s和p,判断s是否能用p进行匹配。

【题解】

dp[i][j]表示s的前i个是否能被p的前j个匹配。

首先可以分成3大类情况,我们先从简单的看起:

(1)s[i - 1] = p[j - 1],易得dp[i][j] = dp[i-1][j-1]

(2)p[i - 1] = '.',因为'.'可以匹配任何字符,所以dp[i][j] = dp[i-1][j-1]

(3)p[i - 1] = '*',这种情况就比较复杂了。

当p[j - 2] != s[i - 1] && p[j - 2] != '.'时,即当s = aba,p = abb*这种情况,因为 * 可以代表0个或多个,所以当p[j -2] != s[i - 1]时,往回倒退2个即dp[i][j] = dp[i][j - 2]

然后除去这种情况还有3种匹配情况,只要满足以下任意一种情况也是成立的

当 * 匹配多个时:dp[i][j] = dp[i - 1][j],即当s = abbb,p = ab*,因为b相等所以可以将s最后一个b删去,比较前面的abb与ab*从而实现 * 表示多个

当 * 匹配单个时:dp[i][j] = dp[i][j - 1],即当s = aba,p = aba*

当 * 匹配0个时:dp[i][j] = dp[i][j - 2],上面说过了

【代码】

 1 class Solution {
2 public:
3 bool dp[35][35];
4 bool isMatch(string s, string p) {
5 s = " " + s;
6 p = " " + p;
7 memset(dp , false, sizeof(dp));
8 dp[0][0] = true;
9 int m = s.size(), n = p.size();
10 for (int i = 1; i <= m; i++){
11 for (int j = 1; j <= n; j++){
12 if (s[i - 1] == p[j - 1])dp[i][j] = dp[i - 1][j - 1];
13 else if (p[j - 1] == '.')dp[i][j] = dp[i - 1][j - 1];
14 else if (p[j - 1] == '*'){
15 if (s[i - 1] != p[j - 2] && p[j - 2] != '.')
16 dp[i][j] = dp[i][j - 2];
17 else{
18 dp[i][j] = dp[i][j - 1] || dp[i][j - 2] || dp[i - 1][j];
19 }
20 }
21 }
22 }
23 return dp[m][n];
24 }
25 };

【LeetCode】10.Regular Expression Matching(dp)的更多相关文章

  1. 【leetcode】10.Regular Expression Matching

    题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

  2. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  3. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  5. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  6. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. leetcode problem 10 Regular Expression Matching(动态规划)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Regular Expression Matching(正则表达式匹配)

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. Linux bash fi

    Linux bash fi if..else..fi allows to make choice based on the success or failure of a command. if..e ...

  2. Python & file operation mode

    Python & file operation mode create/read/write/append mode https://docs.python.org/3/library/fun ...

  3. js 实现各种算法 APP

    js 实现各种算法 APP 常见算法: 排序,搜索/查找,枚举,遍历,最短路径,二叉树 open source web app desktop app react native app flutter ...

  4. how to group date array by month in javascript

    how to group date array by month in javascript https://stackoverflow.com/questions/14446511/most-eff ...

  5. CSS Dark Mode

    CSS Dark Mode https://kevq.uk/automatic-dark-mode/ https://kevq.uk/how-to-add-css-dark-mode-to-a-web ...

  6. 精密进近OCH的计算

    一.计算步骤 以I类精密进近为例,运行标准的制定大致分为以下几个步骤: 1)确定精密航段的超高障碍物. 2)计算当量高 3)计算高度损失 4)当量高与高度损失相加得到超障高OCH 5)对复飞段障碍物进 ...

  7. 直播预告 | 全面的审计分析和权限管控——CloudQuery年终发布!

    2020年9月,CloudQuery 发布. 针对开发.运维人员面临的如何高效便捷访问.操作管理数据的问题,我们设计并研发了云原生安全数据操作平台,CloudQuery 就此诞生! 2020年结束之际 ...

  8. 完整的 LDAP + phpLDAPadmin安装部署流程 (ubuntu18.04)

    LDAP 安装部署以及基础使用 因工作需求需要使用ldap管理用户权限,在踩了一系列坑之后,总结了一些流畅的文档,希望可以帮到和曾经的我一样迷茫的人. 基础环境:Ubuntu 18.04 一.安装 r ...

  9. Pygame基础(1)

    Pygame是Python的一个很常用的游戏框架,今天我来讲一讲Pygame的基础知识 Pygame的官网:https://www.pygame.org/news Pygame的下载 打开cmd输入p ...

  10. C语言柔性数组和动态数组

    [前言]经常看到C语言里的两个数组,总结一下. 一.柔性数组 参考:https://www.cnblogs.com/veis/p/7073076.html #include<stdio.h> ...