题目


Given an input string(s) and a pattern(p), implement regular expression matching with support for '.' and ''.
  '.' Matches any single character.
  '
' Matches zero or more of the preceding element.

The matching should cover the entire input string(not partial).
Note:
  s could be empty and contains only lowercase letters a-z.
  p could be empty and contains only lowercase letters a-z, and characters . or * .

Example1:
  Input:s = "aa" p="a"
  Output:false
  Explanation:"a" does not match the entire string "a"
Example2:
  Input:s = "aa" p="a*"
  Output:true
  Explanation:".*" means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it become "a".

Example3:
  Input:s = "ab" p=".*"
  Output:true
  Explanation:"." means " zero or more () of any character (.) " .

思路


动态规划

Step1. 刻画一个最优解的结构特征

\(dp[i][j]\)表示\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-1]\)是否匹配

Step2. 递归定义最优解的值

1.\(p[j-1] == s [i-1]\),则状态保存,\(dp[i][j] = dp[i-1][j-1]\)
2.\(p[j-1] ==\) ..与任意单个字符匹配,于是状态保存,\(dp[i][j] = dp[i-1][j-1]\)
3.$p[j-1] == $**只能以X*的形式才能匹配,但是由于*究竟作为几个字符匹配不确定,此时有两种情况:

  • \(p[j-2] != s[i-1]\),此时\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-3]\)匹配,即\(dp[i][j] = dp[i][j-2]\)
  • \(p[j-2] == s[i-1]\) 或者 $p[j-2] == $ .,此时应分为三种情况:
    *作为零个字符,\(dp[i][j] = dp[i][j-2]\)
    *作为一个字符,\(dp[i][j] = dp[i][j-1]\)
    *作为多个字符,\(dp[i][j] = dp[i-1][j]\)
Step3. 计算最优解的值

根据状态转移表,以及递推公式,计算dp[i][j]

Tips


数组初始化(python)

(1)相同的值初始化(一维数组)
#方法一:list1 = [a a a ]
list1 = [ a for i in range(3)]
#方法二:
list1 = [a] * 3
(2)二维数组初始化

初始化一个\(4*3\)每项固定为0的数组

list2 = [ [0 for i in range(3)] for j in range(4)]

C++

class Solution {
public:bool isMatch(string s, string p) {int m = s.length(),n = p.length();bool dp[m+1][n+1];dp[0][0] = true;//初始化第0行,除了[0][0]全为false,因为空串p只能匹配空串,其他都无能匹配for (int i = 1; i <= m; i++)dp[i][0] = false;//初始化第0列,只有X*能匹配空串for (int j = 1; j <= n; j++)dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++){if (p[j - 1] == '*'){dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j];}else //只有当前字符完全匹配,才能传递dp[i-1][j-1] 值{dp[i][j] = (p[j - 1] == '.' || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];}}}return dp[m][n];}
};

Python

def isMatch(self, s, p):""":type s: str:type p: str:rtype: bool"""len_s = len(s)len_p = len(p)dp = [[False for i in range(len_p+1)]for j in range(len_s+1)]dp[0][0] = Truefor i in range(1, len_p + 1):dp [0][i] = i>1 and dp[0][i - 2] and p[i-1] == '*'for i in range (1, len_s + 1 ):for j in range(1, len_p + 1):if p[j - 1] == '*':#状态保留dp[i][j] = dp[i][j -2] or (s[i-1] == p[j-2] or p[j-2] == '.') and dp[i-1][j]else:dp[i][j] = (p[j-1] == '.' or s[i-1] == p[j-1]) and dp[i-1][j-1]return dp[len_s][len_p]

转载于:https://www.cnblogs.com/Jessey-Ge/p/10993447.html

10. Regular Expression Matching[H]正则表达式匹配相关推荐

  1. 【重点 递归 动态规划 正则表达式匹配】LeetCode 10. Regular Expression Matching

    LeetCode 10. Regular Expression Matching 本博客参考:http://www.cnblogs.com/grandyang/p/4461713.html 详细解析见 ...

  2. LeetCode 10. Regular Expression Matching / 44. Wildcard Matching

    10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配   dp[i-1][j-1] ...

  3. leetcode 10 Regular Expression Matching

    题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...

  4. 【LeetCode】10. Regular Expression Matching

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

  5. LeetCode Regular Expression Matching(.和*通配符匹配)

    给出text和pattern,pattern中可以使用通配符.和*,.表示匹配任意字符,*表示匹配上一个字符 从text和pattern的第一个字符开始做匹配 1.第一个字符匹配成功,要么第一个字符相 ...

  6. LeetCode 10 Regular Expression Matching(字符串匹配)

    题目链接 https://leetcode.com/problems/regular-expression-matching/?tab=Description   '.' Matches any si ...

  7. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  8. LeetCode:10. Regular Expression Matching

    老大难问题,终于算是理解了. 首先状态定义不难写(哈哈): dp[i][j]: s[0->i-1] p[0->j-1],它们是不是符合. 难的是状态转移方程(又一次验证): 1.如果s[i ...

  9. 【算法】Regular Expression Matching 正则匹配

    [算法]Regular Expression Matching 正则匹配 题目 解题思路 代码实现 题目 Given an input string ( s ) and a pattern ( p ) ...

  10. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

最新文章

  1. textView代码设置文字居中失效 textView设置文字居中两种方法
  2. 如何在Eclipse和Android Studio中导入library project
  3. 创建可按比例调整的布局的 Windows 窗体
  4. 怎么撤回操作_微信又更新,拍一拍能撤回了
  5. 【转】android TV CTS 4.0.3_r1测试
  6. bxslider 纵向滑动 vertical image thumbnail slider
  7. java中数组合并的方法,数组合并--Java原生方法
  8. 【Flutter】基础组件【01】Text
  9. java-判断集合中的某个元素的属性是否全部相同
  10. Akka-CQRS(15)- Http标准安全解决方案:OAuth2+JWT
  11. c语言递归思想实践-整形数组求极值问题
  12. 传奇世界服务端WIN7简单安装教程
  13. 【数据结构】算法的时间复杂度和空间复杂度解析
  14. 教你编写一个手势解锁控件
  15. 人过青年,我们的黄金时代过去了吗?
  16. 最常去的IT网站(技术类,资讯类,个人博客)
  17. 怎样成为一名优秀的程序员
  18. 58同城陈小华:互联网新浪潮就是O2O
  19. Win8换徽标亮点功能全面整合大阅兵
  20. Java项目第11期-宠物医院管理系统【毕业设计】

热门文章

  1. 用计算机算3次根号0.00005,数值分析复习题13
  2. asp.net 获取全部在线用户_提取在线数据的9个最佳网页抓取工具
  3. 工程思想 ——【程序中的二进制】
  4. 【李宏毅机器学习】03:误差Error
  5. 再论数据仓库与数据库的区别
  6. ZeroMQ设置超时等待
  7. c++反转字符,算法优化与实现
  8. jTip定制实现博客日历
  9. 财务自由之路读书笔记二
  10. 源码安装apache后将其设置为开机启动