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. centos7开机界面出现多个选项

    第一个选项正常启动,第二个选项急救模式启动(系统出项问题不能正常启动时使用并修复系统) 在CentOS更新后,并不会自动删除旧内核.所以在启动选项中会有多个内核选项,可以手动使用以下命令删除多余的内核 ...

  2. [转]连连看游戏 C#

    源代码下载地址 http://files.cnblogs.com/files/z5337/%E8%BF%9E%E8%BF%9E%E7%9C%8B%E6%B8%B8%E6%88%8F.rar 代码由 & ...

  3. 【Codeforces】CF 8 C Looking for Order(状压dp)

    题目 传送门:QWQ 分析 这种题不会做 吃枣药丸..... 想到状压已经经过的点. 然后更新时枚举两个点加进去. 复杂度$  {O(2^n \times n^2)}$. 凉凉. 真正的做法是每一个状 ...

  4. MiniDump产生工具

    1:分析程序异常等等信息,在入口处初始化即可 //生成Dump文件信息 OS:Windows #pragma once #include <windows.h> #include < ...

  5. ORACLE V$lock视图TYPE,ID1,ID2取值的含义

    在oracle v$lock视图中,下面对type,ID1,ID2三个列的具体含义说明下: TYPE   有TM,TX两种类型,TX为行级锁,事物锁,TM锁为表级锁 TYPE ID1 ID2 TM 被 ...

  6. 并发工具类(一)等待多线程的CountDownLatch

    前言   JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...

  7. php中点击链接直接下载图片

    最近需要一个功能,是点击链接,直接把图片下载下来,一般情况下,图片是在新页直接打开的,不会自动提示下载,在网上找来找,用这个挺好使,代码如下: $filename = basename($downfi ...

  8. 9. MyEclipse中的SVN操作手册

     该文章转载出处:http://blog.sina.com.cn/s/blog_8a3d83320100zhmp.html 1.导入项目 点击工具栏上的[File-Import],进入下图 (如果你的 ...

  9. 9. PD逆向工程--由数据库转为模型(ER图)

    步骤: 1. 在控制面板-->管理工具(如果没找到管理工具,查看方式改为大图标)-->数据源(ODBC)-->用户DSN -->用户数据源下添加一个数据源(这里根据情况添加数据 ...

  10. jssip中文开发文档(完整版)

    jsSip开发文档 (官网地址:http://www.jssip.net/) 完整案例demo下载地址: http://download.csdn.net/download/qq_39421580/1 ...