题意:

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).The function prototype should be:
bool isMatch(const char *s, const char *p)Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true   (hard)

分析:  题目意思是  正则表达式匹配,'.'匹配任意字符, '*'表示前一个字符可以出现任意多次(0次,1次,2次...)如ab 与 .*  :将.出现两次为..可以匹配任意字符,所有可以匹配ab aab 与 c*a*b :c*将c出现0次, a*将a出现两次,得到aab可以匹配。

而且特殊符号是只出现在p字符串中的(开始没理解这个导致感觉问题很复杂推不下去...,可能因为没学过正则表达式)

解析: 采用动态规划。双序列动态规划常见得状态选取即为dp[i][j]表示s的前i个与p的前j个...对应于本题,dp[i][j]表示s的前i个字符与p的前j个字符能否匹配。 但本题递推关系不是很好推完整(毕竟hard)当p[j-1] != '*'    需要s[i-1] == p[j-1] 并且 dp[i-1][j-1] == true (前i-1个与前j-1个能匹配)当p[j-1] == '*'    有如下几种情况:    1)*前的字符需要重复0次 例如匹配 ab 和 aba*, 该情况下dp[i][j]是否为真取决于 dp[i][j-2] 是否为真;    2)*前的字符需要重复1次,即其本身,如匹配 aba 和 aba*, 该情况下dp[i][j]是否为真取决于dp[i][j-1]是否为真;    3)*前字符需要重复2次或以上, 如匹配 abaa 与 aba*(出现两次), 匹配aaa与.*(出现大于两次);      该情况下需要s[i-1] == p[j-2] && (dp[i-1][j-1] || dp[i-1][j]) // dp[i-1][j]容易忽略,表示要利用*前元素大于两次;

初始化dp[0][0], dp[i][0] (i = 1,2,...s.size()) , dp[0][j], (j = 1,2,...p.size());其中dp[0][j]的需要点判断, p[j - 1] == '*' &&  dp[0][j - 2] (即*帮助去掉了前面的字符。开始还写了||dp[0][j-1],后来发现*不会打头存在,所以dp[0][j-1]没必要)

代码:(还有一些小细节在注释中)
 1 class Solution {
 2 public:
 3     bool isMatch(string s, string p) {
 4         bool dp[s.size() + 1][p.size() + 1];
 5         dp[0][0] = true;
 6         for (int i = 1; i <= s.size(); ++i) {
 7             dp[i][0] = false;
 8         }
 9         for (int j = 1; j <= p.size(); ++j) {
10              //p[j-2]一定存在,*不会打头!
11              if (p[j - 1] == '*' &&  dp[0][j - 2])  {
12                 dp[0][j] = true;
13             }
14             else {
15                 dp[0][j] = false;
16             }
17         }
18
19         for (int i = 1; i <= s.size(); ++i) {
20             for (int j = 1; j <= p.size(); ++j) {
21                 if (p[j - 1] != '*') {
22                     dp[i][j] =  (s[i - 1] == p[j - 1] || p[j - 1] == '.') && dp[i - 1][j - 1];
23                 }
24                 else {
25                     //重复两次或更多   dp[i-1][j]  如:aaa与.*
26                     // * 不会打头,所以p[j - 2]一定存在
27                     bool b1 = (s[i - 1] == p[j - 2] || p[j - 2] == '.') && (dp[i - 1][j - 1] || dp[i - 1][j]);
28                     bool b2 =  dp[i][j - 1];  //重复1次
29                     bool b3 =  dp[i][j - 2];  //重复0次
30                     dp[i][j] = b1 || b2 || b3;
31                 }
32             }
33         }
34         return dp[s.size()][p.size()];
35     }
36 };

学习了一下讨论区,发现递推的情况基本是一样的,有一点细微的优化。

我做的时候是先想到类似abaa 与 aba*, *前字符重复两次,所以写出了dp[i - 1][j - 1]。(标红前半句)提交之后WA发现有情况没考虑到,即aaa与.* 即出现大于两次,所以添加了dp[i-1][j]而现在仔细考虑,实际上只需要一句dp[i-1][j]就可以处理大于等于2次重复的情况,所以标红句可以优化为
bool b1 = (s[i - 1] == p[j - 2] || p[j - 2] == '.') &&  dp[i - 1][j];

 

转载于:https://www.cnblogs.com/wangxiaobao/p/5758986.html

LeetCode10 Regular Expression Matching相关推荐

  1. leetcode10:Regular Expression Matching

    1.题目如下: '.' Matches any single character. '*' Matches zero or more of the preceding element.The matc ...

  2. Regular Expression Matching

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

  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 10. Regular Expression Matching

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

  6. 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] ...

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

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

  8. Q10 Regular Expression Matching

    Given an input string (s) and a pattern §, implement regular expression matching with support for '. ...

  9. Leetcode Q10: Regular Expression Matching

    题目10: (该题目拿到手没什么特别好的思路,从网上看的别人的解法,然后写了下自己的理解,需要常回顾) Implement regular expression matching with suppo ...

  10. LeetCode Regular Expression Matching

    '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching sh ...

最新文章

  1. ios 圆形旋转菜单_iOS高级动画:圆形树展开收起动画
  2. Skype for Business Server 2015-03-后端服务器-1-安装
  3. 计算机用于尖端科技,【判断题】用演绎法教问句的方法适用于中高级型学生
  4. boost::hana::lift用法的测试程序
  5. Java集合(八) 迭代器Iterator、泛型、Map映射
  6. 牛客练习赛50 F tokitsukaze and Another Protoss and Zerg
  7. 密码学专题 相关概念的解析 对称算法|算法的安全性|非对称算法存在的问题|单向散列函数|数字签名的弊端|密钥交换
  8. C++多态讲解以及常见面试题
  9. java nio copy_使用NIO快速复制Java文件
  10. 任何举动之前,先思考,思考,再思考
  11. html5里面em是什么单位,HTML5中单位em的理解
  12. java基础之算法_java基础之几种常见的排序算法
  13. 常见关联图库之欺诈指数排位战
  14. JavaScript常用事件(1)
  15. html网页纯静态花店购物网站源码div+css页面将计30页,大学生毕业设计源码(源码下载)
  16. 软件加密狗破解思路和方法
  17. linux磁盘文件检查修复工具下载,Linux磁盘坏道的检测及修复
  18. 如何集成指纹验证到网站系统
  19. java锟斤拷锟斤拷锟_锟斤拷?UTF-8与GBK互转乱码问题
  20. 【Basis】狄利克雷分布

热门文章

  1. Docker Redis 安装
  2. log4j的使用配置
  3. Entity Framework之IQueryable和list本地集合
  4. 学好 Python 的 11 个优秀资源【转载】
  5. 二十岁出头,你一无所有,但你却拥有一切
  6. windows下安装Perl模块
  7. .NET 4 实践 - 使用dynamic 和MEF实现轻量级的 AOP 组件 (1)
  8. 利用XMLHTTP无刷新添加数据之Post篇(转)
  9. 腾讯2017暑期实习编程题3
  10. iOS开发-停止WebView播放视频/音频