这个题目,我从前天晚上(8月6号晚上)调试到现在(8月8号16:21),太心酸了,不好好总结一下,就太对不起自己了!
这是题目:

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
动态规划,其实任何算法都是:核心思想+边界条件,其实不少的边界条件还是在调试过程中加上去的!!
该题,我参考了网上的不少博客,自己总结如下:
     首先用一个二元数组来记录s和p的匹配情况(s代表源字符串,p代表目标字符串),比如res[i][j]为true表示 s[0... i-1]与p[0... j-1]已经匹配。
     假设当前需要判定s[0 ...i]与p[0... j]是否匹配,也就是res[i+1][j+1]是否为true:
  •      若p[j]=='*'

               (1)若p[j-1]!='.'  则将res[i+1][j+1]置为true,只需满足以下3个条件当中的任意一个:

                         a. res[i+1][j]为true(此时*将其前面的那个字符只取1次)
..... i-1 i          
..... j-1 j       *    
                         b. res[i+1][j-1]为true(此时*将其前面的那个字符一次也不取,即二者为空了)
                         c. res[i][j+1]为true且s[i]==s[i-1]且s[i-1]==p[j-1]。(此时'*'将其前面的一个字符取了2次)。而且c选项还可以继续递归下去。
                   (2)若p[j-1]=='.'
                              因为'.*'代表0个或多个'.',这可以匹配任何字符,所以只要res[i+1][j-1]或者res[i+1][j]中任意一个为true,剩下的res[i+1][j+1],res[i+2][j+1]......res[s.length()][j+1]就都可置为true。
  • 若p[j]!='*'
                    当(s[i]==p[j]或p[j]=='.')且res[i][j]为true时,res[i+1][j+1]置为true。
 
     最后return res[s.lenth()][p.length()]
 
 
下面这些是我主要的参考资料,先附上自己的代码:
 class Solution {
public:
bool isMatch(string s, string p) {
//constexpr int len1 = static_cast<const int>(s.length()), len2 = p.length();
//bool res[len1][len2] = { 0 }; 这儿自己原本是想直接用数组,但是数组下标是要求常量表达式的,着急啊,现在也没解决
int len1 = s.length() + , len2 = p.length() + ;
vector<vector<bool>> res(len1, vector<bool>(len2, false));
res[][] = true;
for (int i = ; i < len2;i++)//没有这3句, "aab", "c*a*b" 会不通过
if (p[i - ] == '*')
res[][i] = res[][i - ]; for (int j = ; j < len2-; j++)
{ if (p[j] == '*')
{ if (j>&&p[j - ] != '.')
{
for (int i = ; i < len1-; ++i)
if (res[i + ][j - ] || res[i + ][j] ||i>&& s[i - ] == s[i] && s[i - ] == p[j - ]&& (res[i][j + ]||res[i][j]))//这个地方一定要注意在具体的条件上加上限制就好了,千万别去将前面的for循环由i=0改为i=1
res[i + ][j + ] = true;
}
else
{
int i = ;
// for (; i < len1;)
//if (!res[i+1][j - 1] && !res[i][j])
// ++i; 这个地方竟然写了个死循环
while (j>&&i < len1-&&!res[i + ][j - ] && !res[i+][j])
++i;
for (; i < len1-; ++i)
res[i + ][j + ] = true; }
}
else
{ for (int i = ; i < len1-;++i)
if ((s[i] == p[j] || p[j] == '.') && res[i][j])
res[i + ][j + ] = true;
}
}
return res[len1 - ][len2 - ];
}
};
 

regular expression matching DP的更多相关文章

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

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

  2. 10.Regular Expression Matching (String; Back-Track,DP)

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

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

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

  4. [Leetcode][Python][DP]Regular Expression Matching

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...

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

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

  6. [LeetCode] 10. Regular Expression Matching

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

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

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

  8. 【 Regular Expression Matching 】cpp

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

  9. LeetCode10 Regular Expression Matching

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

随机推荐

  1. 构建Docker平台【第一篇】环境准备

    主机信息 操作系统版本 CentOS-7-x86_64-Everything-1511   主机A 192.168.6.128 主节点 主机B 192.168.6.129 主节点 主机C 192.16 ...

  2. HBase之七:事务和并发控制机制原理

    作为一款优秀的非内存数据库,HBase和传统数据库一样提供了事务的概念,只是HBase的事务是行级事务,可以保证行级数据的原子性.一致性.隔离性以及持久性,即通常所说的ACID特性.为了实现事务特性, ...

  3. 【旧文章搬运】Windbg+Vmware驱动调试入门(三)---Windbg基本调试入门

    原文发表于百度空间,2009-01-09========================================================================== 这一节的内 ...

  4. UI:数据库练习、滤镜效果

    相机处理滤镜效果 滤镜主要使用在相机的美颜. #import "ViewController.h" #import "ImageUtil.h" #import ...

  5. chromium浏览器开发系列第一篇:如何获取最新chromium源码

    背景:      最近摊上一个事儿,领导非要让写一篇技术文章,思来想去,自己接触chrome浏览器时间也不短了,干脆就总结一下吧.于是乎,本文顺理成章.由于有些细节必需描述清楚,所以这次先讲如何拿到c ...

  6. Codechef SUMCUBE

    SUMCUBE code 给定无向简单图 G = (V, E)(即不存在自环和重边),以及 k = 1, 2, 或3 .求$$ \sum_{S \subseteq V} f(S)^k, $$其中 $f ...

  7. Android.mk中call all-subdir-makefiles和call all-makefiles-under,$(LOCAL_PATH)的区别(转载)

    转自:http://blog.csdn.net/jackyu613/article/details/5949324 在写Android.mk文件时,call all-subdir-makefiles和 ...

  8. 洛谷 - P2444 - 病毒 - AC自动机

    https://www.luogu.org/problemnew/show/P2444 有点恶心,不太明白fail的意义. #include<bits/stdc++.h> using na ...

  9. Androidstudio中添加jar包的方法

    在Androidstudio中添加一个jar包进去,怎么添加? 以下纯个人使用Androidstudio过程中的经验积累,要是有不足,望提出建议. 方法一: 先点击Androidstudio中的Pro ...

  10. bzoj 3811: 玛里苟斯【线性基+期望dp】

    这个输出可是有点恶心啊--WA*inf,最后抄了别人的输出方法orz 还有注意会爆long long,要开unsigned long long 对于k==1,单独考虑每一位i,如果这一位为1则有0.5 ...