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 ...
随机推荐
- js运动 淡入淡出
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 让CSS3给你的文字加上边框宽度,并实现镂空效果
这次,我主要向大家介绍一下CSS3下的-webkit-text-stroke属性,并分享几个用该属性制作的镂空文字效果. 1.-webkit-text-stroke属性简介 CSS边框的一个不足就是只 ...
- 在VS2012中实现Ext JS的智能提示
Visual Studio 2012太强大了,居然能自己会去提取Ext JS的类的属性和方法,从而实现只能提示.下面就来介绍一下实现这个功能. 在Visual Studio 2012中随便创建一个We ...
- JS中如何判断null、undefined与NaN
1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp) ...
- POJ1384Piggy-Bank(DP)
POJ1384http://poj.org/problem?id=1384 最简单的完全背包问题,注意下初始化为INF就可以. #include <map> #include <se ...
- nginx打开目录游览功能
#开启索引功能 location / { autoindex on; autoindex_exact_size off; autoindex_localtime on; } #别名目录location ...
- hdu 3397 Sequence operation(很有意思的线段树题)
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 对于 Javascript 闭包理解
一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量 ...
- Tair分布式key/value存储
[http://www.lvtao.net/database/tair.html](特别详细) tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化 ...
- Jupyter增加内核
本例的Jupyter安装在Python3下,以增加Python2内核为例. 首先确认在Python3下已安装了内核: ipython kernel install --user #or python3 ...