题意:

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的更多相关文章

  1. [Swift]LeetCode10. 正则表达式匹配 | Regular Expression Matching

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

  2. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  3. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  4. No.010:Regular Expression Matching

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

  5. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【leetcode】Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  7. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 66. Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  9. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

随机推荐

  1. Hadoop开发环境简介(转)

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  2. 使用google map v3 api 开发地图服务

    Google Map V3 API 学习地址: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/article ...

  3. 随笔2 PAT1001.A+B Format (20)

    1001.A+B Format(20) 题目链接 1001.A+B Format (20) C++ 代码 第一次使用markdown,还不是很习惯,现在努力的在适应它 首先这道题我们很容易就可以读懂题 ...

  4. Codeforces 706 D. Vasiliy's Multiset (字典树贪心)

    题目链接:http://codeforces.com/contest/706/problem/D 题意很简单不多说. 把一个数当作二进制插入字典树中,按照xor的性质,1找0,0找1,贪心即可. 注意 ...

  5. HDU 4870Rating(推公式)

    有关这个题的高斯消元的方法已经在我的另一篇博客中给出http://www.cnblogs.com/gj-Acit/p/3888382.html 这里介绍一个很吊的解法,复杂度降到了O(n),以下转自h ...

  6. Remove Duplicates from Sorted List @LeetCode

    /** * Remove Duplicates from Sorted List * * Given a sorted linked list, delete all duplicates such ...

  7. 小巧实用js倒计时

    <script type="text/javascript">     var intDiff = parseInt(15); //倒计时总秒数量     functi ...

  8. OC三种方法实现定时器

    在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 在iOS中有很多方法完成定时器的任务,例如 NSTimer.CADisp ...

  9. 使用XCopy发布网页

    链接:https://documentation.devexpress.com/#eXpressAppFramework/CustomDocument113245 In this lesson, yo ...

  10. 用jquery判断当前显示器的分辨率,加载不同CSS

    <link rel="stylesheet" type="text/css" id="css"><script langu ...