LeetCode10 Regular Expression Matching
题意:
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]没必要) 代码:(还有一些小细节在注释中)
class Solution {
public:
bool isMatch(string s, string p) {
bool dp[s.size() + ][p.size() + ];
dp[][] = true;
for (int i = ; i <= s.size(); ++i) {
dp[i][] = false;
}
for (int j = ; j <= p.size(); ++j) {
//p[j-2]一定存在,*不会打头!
if (p[j - ] == '*' && dp[][j - ]) {
dp[][j] = true;
}
else {
dp[][j] = false;
}
}
for (int i = ; i <= s.size(); ++i) {
for (int j = ; j <= p.size(); ++j) {
if (p[j - ] != '*') {
dp[i][j] = (s[i - ] == p[j - ] || p[j - ] == '.') && dp[i - ][j - ];
}
else {
//重复两次或更多 dp[i-1][j] 如:aaa与.*
// * 不会打头,所以p[j - 2]一定存在
bool b1 = (s[i - ] == p[j - ] || p[j - ] == '.') && (dp[i - ][j - ] || dp[i - ][j]);
bool b2 = dp[i][j - ]; //重复1次
bool b3 = dp[i][j - ]; //重复0次
dp[i][j] = b1 || b2 || b3;
}
}
}
return dp[s.size()][p.size()];
}
};
学习了一下讨论区,发现递推的情况基本是一样的,有一点细微的优化。 我做的时候是先想到类似abaa 与 aba*, *前字符重复两次,所以写出了dp[i - 1][j - 1]。(标红前半句)
提交之后WA发现有情况没考虑到,即aaa与.* 即出现大于两次,所以添加了dp[i-1][j]
而现在仔细考虑,实际上只需要一句dp[i-1][j]就可以处理大于等于2次重复的情况,所以标红句可以优化为
bool b1 = (s[i - ] == p[j - ] || p[j - ] == '.') && dp[i - ][j];
LeetCode10 Regular Expression Matching的更多相关文章
- [Swift]LeetCode10. 正则表达式匹配 | Regular Expression Matching
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- No.010:Regular Expression Matching
问题: Implement regular expression matching with support for '.' and '*'.'.' Matches any single charac ...
- Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【leetcode】Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- 转】MyEclipse10安装Log4E插件
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4231812.html 感谢! 一. Log4E插件下载 下载地址:http://log4e.jayefem.de/ ...
- 【多线程】Java并发编程:Lock(转载)
原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized ...
- tomcat的host配置
本机 etc\hosts 首先了解C:\WINDOWS\system32\drivers\etc\hosts文件配置 127.0.0.1 static1.ezsins.com #adoble ps c ...
- STM32中的位带(bit-band)操作
转:http://blog.csdn.net/gaojinshan/article/details/11479929 //位带操作,实现51类似的GPIO控制功能 //具体实现思想,参考<< ...
- LightOJ 1245 Harmonic Number (II)(找规律)
http://lightoj.com/volume_showproblem.php?problem=1245 G - Harmonic Number (II) Time Limit:3000MS ...
- 用 const 限定类的成员函数
类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作任何改变. 在设计类的时候,一个原则就是对于不改变数据成员的成员函数都要在后面加 const,而对于 ...
- Computational Geometry Template_Polygon
#include <stdlib.h> #include <math.h> #include <iostream> #define MAXN 1000 #defin ...
- iOS摄像头和相册-UIImagePickerController-浅析
转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html 在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的 ...
- Mahout之Navie Bayesian命令端运行
landen@landen-Lenovo:~/文档/20news$ mahout trainclassifier --helpMAHOUT_LOCAL is not set; adding HADOO ...
- HTTP 错误 405.0 - Method Not Allowed
如果A页面通过表单(form)向B页面传递参数,而B页面是以“.htm or .html ”为扩展名的话,通过IIS解析会出现“HTTP 错误 405 -禁止访问资源”错误的提示. 原因:IIS解析文 ...