10.Regular Expression Matching (String; Back-Track,DP)
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
思路:如果下个字符是'*',那么可以重复当前字符0,1,2...次=>带回溯的递归。O(n)=kn,k是*出现的次数。
class Solution {
public:
bool isMatch(string s, string p) {
return dfsIsMatch(s,p,0,0);
}
bool dfsIsMatch(const string& s, const string& p, int sIndex, int pIndex){
if (p[pIndex] == '\0') //结束条件:s若是'\0',p必须也是'\0'
return s[sIndex] == '\0';
if (p[pIndex+1] == '*') {
/* '.' means any character (except '\0')
* '.' means repeat 0 or more times
* '.*' means repeat '.' 0 or more times
*/
while ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) { //'.'可以与除'\0'以外的任何字符匹配
if (dfsIsMatch(s, p, sIndex, pIndex+2)) //p[pIndex] repeat 0 times
return true;
sIndex += 1;//p[pIndex]在原基础上repeat次数+1
}
return dfsIsMatch(s, p, sIndex, pIndex+2); //when s[sIndex] != p[pIndex] && p[pIndex] != '.'(此时只有s[sIndex]==p[pIndex]=='\0'时可能return true)
}
else if ((s[sIndex] != '\0' && p[pIndex] == '.') || s[sIndex] == p[pIndex]) {
return dfsIsMatch(s, p, sIndex + 1, pIndex + 1);
}
return false;
}
};
法II:动态规划。设二位数组dp[i][j]来表示两个string的状态关系,dp[i][j]=true,表示s[0..i-1]匹配p[0..j-1]的结果。O(n)=n2
dp的下标0表示NULL,设置的原因是,比方如果p的第二个字符是*,那么p可以从第三个字符开始与s的第一个字符匹配,也就是dp[0][2]=true。
class Solution {
public:
bool isMatch(string s, string p) {
int sLen=s.length();
int pLen = p.length();
bool dp[sLen+1][pLen+1]={false}; //[0]for string NULL
//initialize
dp[0][0]=true;
for(int j = 1; j <= pLen; j++){
dp[0][j]=j>=2 && dp[0][j-2] && p[j-1]=='*'; //* each other letter
}
//state transfer
for(int i = 1; i <= sLen; i++){
for(int j = 1; j <= pLen; j++){
if(p[j-1]=='*'){
dp[i][j]=(j>=2 && dp[i][j-2])/* repeat 0 times*/
||(dp[i][j-1])/*repeat 1 time*/
||(j>=2 && (s[i-1]==p[j-2] || p[j-2]=='.') && dp[i-1][j])/*repeat several times*/;
}
else{ //p[j-1]!='*'
dp[i][j]=dp[i-1][j-1] && (s[i-1]==p[j-1] || p[j-1]=='.');
}
}
}
return dp[sLen][pLen];
}
};
10.Regular Expression Matching (String; Back-Track,DP)的更多相关文章
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
随机推荐
- 开发需求:每个表dump出部分数据
mysqldump 每张数据表统一导出的N个记录数 需求: 在开发过程中,需要将生产的数据导出到本地开发环境,希望可以每个表都导出部分数据.由于生产数据表都是上千万或亿级别,直接从服务器导出数据会影响 ...
- 最近比赛中遇到的几道dp题
1.2015 icpc 长春-H-Partial Tree(据说是完全背包,但我觉得不像) 一.题意 给定$n$个点,每一个点$i$的权值为关于度数$d_i$的函数$f(d_i),$让你构建一棵树,使 ...
- 【BZOJ】1975 [Sdoi2010]魔法猪学院(A*)
题目 传送门:QWQ 分析 k短路,Astar.估价函数是终点向外跑的最短路. 显然不是正解qwq. 代码 // By noble_ // Astar algorithm // #include &l ...
- CentOS7 安装supervisor守护进程管理器
supervisor没有发布在标准的CentOS源在,需要安装epel源.这种方式安装的可能不是最新版本,但比较方便,安装完成之后,配置文件会自动帮你生成. 默认配置文件:/etc/superviso ...
- spring-mvc注解配置小记
Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...
- Flex知识
转载:http://www.cnblogs.com/xia520pi/archive/2011/12/11/2283851.html
- Solr如何使用in语法查询
Solr可以用AND.|| 布尔操作符 表示查询的并且, 用OR.&& 布尔操作符 表示或者 用NOT.!.-(排除操作符不能单独与项使用构成查询)表示非 如果要用在查询的时候使用 ...
- ie6 双边距问题
div 设置float和margin margin在float方向上会变成设置值的两倍 解决方法,加上css _display:inline
- Tomcat 支持的Java 版本和兼容性总结
https://tomcat.apache.org/whichversion.html 最新最全的Tomcat 支持的Java版本对照,即兼容性一览表: Servlet Spec JSP Spec ...
- servlet练习1
1. 编写一个Servlet,当用户请求该Servlet时,显示用户于几点几分从哪个IP(Internet Protocol)地址连线至服务器,以及发出的查询字符串(Query String).查询一 ...