【题意】

给两个字符串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. URL parser All In One

    URL parser All In One const url = new URL(`https://admin:1234567890@cdn.xgqfrms.xyz:8080/logo/icon.p ...

  2. 最新 Vue 源码学习笔记

    最新 Vue 源码学习笔记 v2.x.x & v3.x.x 框架架构 核心算法 设计模式 编码风格 项目结构 为什么出现 解决了什么问题 有哪些应用场景 v2.x.x & v3.x.x ...

  3. 十大排序算法时间复杂度 All In One

    十大排序算法时间复杂度 All In One 排序算法时间复杂度 排序算法对比 Big O O(n) O(n*log(n)) O(n^2) 冒泡排序 选择排序 插入排序 快速排序 归并排序 基数排序 ...

  4. ASCII Art

    ASCII Art https://npms.io/search?q=ASCII art ASCII Art Text to ASCII Art Generator (TAAG) http://pat ...

  5. css & auto height & overflow: hidden;

    css & auto height & overflow: hidden; {overflow: hidden; height: 100%;} is the panacea! {溢出: ...

  6. export excel

    export excel sheet.js https://sheetjs.com/ https://github.com/SheetJS/sheetjs excel.js https://www.n ...

  7. URLSearchParams & GET Query String & JSON

    URLSearchParams & GET Query String & JSON https://developer.mozilla.org/zh-CN/docs/Web/API/U ...

  8. wireshark 获取指定进程id的数据

    >netstat -aon | findstr 11380 TCP 191.127.1.7:57936 29.225.107.216:3734 ESTABLISHED 11380 过滤器: tc ...

  9. ng 基础

    文档 组件的工作只管用户体验,而不用顾及其它. 它应该提供用于数据绑定的属性和方法,以便作为视图和应用逻辑的中介者 组件应该把诸如从服务器获取数据.验证用户输入或直接往控制台中写日志等工作委托给各种服 ...

  10. C++算法代码——质数的和与积

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1682 题目描述 两个质数的和是S,它们的积最大是多少? 输入 输入文件名为prime ...