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 ...
随机推荐
- sysctl.conf
linux系统接口 允许改变正在运作linux系统接口Tcp/IP堆栈和虚拟内存系统的高级选项 用来控制Linux网络配置/proc/sys/net/core/ TCP/IP参数修改添加到/etc/s ...
- (转载)Java里快如闪电的线程间通讯
转自(http://www.infoq.com/cn/articles/High-Performance-Java-Inter-Thread-Communications) 这个故事源自一个很简单的想 ...
- Intellij IDEA 杂记
添加JUnit File > Settings > Plugins > Browse repositories > 搜索junit ,安装JunitGenerator V2 重 ...
- oracle存储过程返回数据集结果
MSSQL的存储过程返回数据集好简单,直接SELECT 就可以. ORACLE的存储过程返回数据集必须通过游标. 创建ORACLE存储过程 create or replace procedure cx ...
- 接口是干爹, 继承是亲爹 ---JAVA
接口(interface)是干爹, 因为你可以有很多很多的干爹爹... 继承(extends)是亲爹, 因为你只能有一个父类, 只有一个亲生的父亲. 单继承,多接口?./>./..
- Kafka学习记录
1 Kafka的基本介绍 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.具有快速.可扩展.分布式.可复制等特点.Kafka与传 ...
- 给Oracle锁住的行解锁
1.找出数据库的serial#,以备杀死: select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked_object t1,v$s ...
- Linux环境下oracle创建和删除表空间及用户
#su - oracle $ sqlplus /nolog SQL> connect / as sysdba --//创建临时表空间 create temporary tablespace te ...
- Windows转到linux中,文件乱码,文件编码转换
最近,学习又重新开始Linux学习,所以一直在Centos中,昨天一朋友把他在Windows下写的C程序发给我,我欣然答应,本以为很快就能在我的Linux系统中运行起来.没想到出现了乱码,结果想把这个 ...
- d3.js <一>
<html> <head> <meta charset="utf-8"> <title>HelloWorld</title&g ...