这个题目,我从前天晚上(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. 百度之星资格赛 1004 度度熊的午饭时光(01背包+最小序号和+字典序+有bug)

    分析 首先声明一下,我的代码有漏洞的,求大神给个正确代码 思路如下: 首先做一遍01背包记录路径并求出最大总分,令path[i][j]表示第i个物品包含在dp[j]的求值过程中.再逆序枚举money, ...

  2. linux 读取部分文件内容

    一般我们在linux上读取文件,是用vi工具,如果是写shell 脚本时,一般式通过cat 再 使用管道来达到流的处理. 但如果文件太大,单纯的cat 可能会使用过多的内存,而且实现上还需要后续的加工 ...

  3. android内存溢出 java.lang.OutOfMemoryError

    今天在做ListView 的时候.想做一个音乐列表模块,前面是图片,后面是分类名称,如下图: 结果运行时候,LogCat是总是报 java.lang.OutOfMemoryError的错误,顾名思义, ...

  4. 第十七篇 .NET高级技术之XML

    Xml 简介(可扩展标记语言) XML优点:容易读懂:格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎的编写. Xml就是用一种格式化的方式来存储数据,我们可以通过用记事本打开. .n ...

  5. Gist使用经验

    注:本文只是分享Gist使用经验,不讨论类似软件或服务的优劣,对于技术或软件不要有傲慢与偏见 一.Gist是什么 关于Gist的详细介绍,请阅读官方文档About gists,下面只简略介绍我所用到的 ...

  6. Hive导入10G数据的测试

    Hive导入10G数据的测试 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让Hadoop集群跑在VPS虚拟主机上,通过云向用户提供存储和计算的服务. 现在硬件越来越便宜,一台非 ...

  7. hdu 1558 Segment set 计算几何+并查集★

    #include <cstdio> #include <iostream> #include <string.h> using namespace std; ; # ...

  8. bzoj 5019 [Snoi2017]遗失的答案

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5019 题解 如果L不是G的倍数 答案为0 下面考虑G|L的情况 将G,L质因数分解 设$L= ...

  9. Chef and Graph Queries CodeChef - GERALD07

    https://vjudge.net/problem/CodeChef-GERALD07 可以用莫队+带撤销并查集做 错误记录: 1.调试时数组开小了,忘了改大就交了 2.88行和91行少了备份num ...

  10. SpringCloud开发学习总结(八)—— API网关服务Zuul(一)

    大多数情况下,为了保证对外服务的安全性,我们在服务端实现的为服务接口时往往都会有一定的权限校验机制,比如对用户登录状态的校验等:同时为了防止客户端在发起请求时被篡改等安全方面的考虑,还会有一些签名校验 ...