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

  1. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  2. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  3. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  4. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

  5. 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. leetcode problem 10 Regular Expression Matching(动态规划)

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

  7. 10. Regular Expression Matching

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

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

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

  9. [LeetCode] 10. Regular Expression Matching

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

随机推荐

  1. 1011 World Cup Betting (20 分)

    1011 World Cup Betting (20 分) With the 2010 FIFA World Cup running, football fans the world over wer ...

  2. Linux下不同颜色文件的类型

    蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件:主要是使用ln命令建立的文件 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: 黄色是设备文件,包括block, ch ...

  3. DeepFM模型理论及代码实现

    论文地址:DeepFM: A Factorization-Machine based Neural Network for CTR Prediction

  4. 【POJ】2420 A Star not a Tree?(模拟退火)

    题目 传送门:QWQ 分析 军训完状态不好QwQ,做不动难题,于是就学了下模拟退火. 之前一直以为是个非常nb的东西,主要原因可能是差不多省选前我试着学一下但是根本看不懂? 骗分利器,但据说由于调参困 ...

  5. Hadoop2.0构成之HDFS2.0

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  6. GO ‘N’ Times,SQL执行同一个语句多次

    GO (Transact-SQL)   语法   GO [count] 参数 count 为一个正整数. GO 之前的批处理将执行指定的次数.   源文档 <http://msdn.micros ...

  7. 序列化模块json--pickle--shelve

    什么是序列化? 将一组或多组数据结构转化成一个字符串的过程就叫做序列化 它的目的: 序列化的结构是字符串,准确的说是bytes类型,方便存储 方便于网络传输, 既然序列化是从数据类型到字符串的过程,那 ...

  8. html:模板

    http://www.mycodes.net/code_previewmap.php?id=3461 http://www.17sucai.com/pins/4120.html  欧美风格的CMS企业 ...

  9. CENTOS系统安装及初始化配置相关

    一,配置网卡: 1,设置网卡ip地址:vi   /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yes ...

  10. 57. 激活office时出下以下问题的解决方案

      我们拿出一段来分析一下(我所知道的不多)SKU ID:1b686580-9fb1-4b88-bfba-eae7c0da31adLICENSE NAME: Office 15, OfficeProP ...