regular expression matching 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
- 若p[j]=='*'
(1)若p[j-1]!='.' 则将res[i+1][j+1]置为true,只需满足以下3个条件当中的任意一个:
..... | i-1 | i | |||
..... | j-1 | j * |
- 若p[j]!='*'
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的更多相关文章
- 《LeetBook》leetcode题解(10): Regular Expression Matching——DP解决正则匹配
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 10.Regular Expression Matching (String; Back-Track,DP)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】Regular Expression Matching (hard) ★
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【 Regular Expression Matching 】cpp
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- LeetCode10 Regular Expression Matching
题意: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
随机推荐
- JAVA RTTI
基础类可接收我们发给派生类的任何消息,因为两者拥有完全一致的接口.我们要做的全部事情就是从派生上溯造型,而且永远不需要回过头来检查对象的准确类型是什么.所有细节都已通过多态性获得了完美的控制. 但经过 ...
- AndroidStudio修改主题外观和字体大小
修改主题外观 File --> Settings --> Appearance & Behavior --> Appearance 右边 Theme 修改编辑器的字体大小 F ...
- 【旧文章搬运】更正一个枚举PspCidTable时的错误
原文发表于百度空间及看雪论坛,2009-02-27 看雪论坛地址:https://bbs.pediy.com/thread-82919.htm============================= ...
- E20180404-hm
reject vt. 拒绝; 排斥; 抛弃,扔掉; 吐出或呕吐; conditioner n. 调节器,调节装置 fabric n. 织物; 布; 构造; (建筑物的) 结构(如墙.地面.屋顶):质 ...
- E20180327-hm
renew vt. 补充; 重新开始; 使更新; 使恢复; vi. 重申,重复强调; 重新开始; renewal n. 重建,重生; 更新,革新; 重申; 合同的续订;
- AssetDatabase文档翻译
AssetDatabase是一个能获取工程资源的API,它提供一些方法比如:查找.加载.创建.删除和修改.Unity需要了解工程文件夹里的所有改变,假如想要获取或修改资源文件,就使用 AssetDat ...
- poj 2412 The Balance 【exgcd】By cellur925
题目传送门 一遇到数学就卡住,我这是怎么肥4...(或许到图论会愉悦吧,逃) Description * 给出两种重量为的 A, B 的砝码,给出一种使用最少的砝码的方式,称出重量 C. 我们可以比较 ...
- C#递归拷贝文件夹下文件以及文件夹
public bool CopyOldLabFilesToNewLab(string sourcePath, string savePath) { if (!Directory.Exists(save ...
- 【Nginx】解决Post请求变Get的问题
默认情况下Nginx会把post请求做一次重定向操作,然后后端收到的就成了Get请求,还会导致一些参数的遗漏. 日志如下: 172.16.1.108 - - [11/Jan/2019:18:27:09 ...
- nginx媒体压缩
1 gzip模块 参考:http://nginx.org/en/docs/http/ngx_http_gzip_module.html 浏览器的请求头里会表明Accept-Encoding 方式.服务 ...