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 ...
随机推荐
- C:static 关键字、静态变量、跨类访问数据
static 在OC中的使用 参考1 参考2 参考3 参保4 参考5 跨类访问成员 参考 +号方法就是类方法(静态方法),说明不用创建对象,可以直接通过类型去直接调用这个方法,在OC ...
- C# 利用Aspose.Slides.dll将本地ppt文档转化成pdf(完美破解版 无水印 无中文乱码)
下载Aspose.Slides.dll http://pan.baidu.com/s/1kVPjnzL 添加引用C#代码. using System; using System.Collectio ...
- word-break: break-all word-break:keep-all word-wrap: break-word三者的区别
word-break属性:指定非CJK脚本的断行规则. 值 描述 normal 使用浏览器默认的换行规则. break-all 允许在单词内换行. keep-all 只能在半角空格或连字符处换行. w ...
- Ruby 全局变量,实例变量,类变量
class Computer $manufacturer = "Mango Computer, Inc." # “$" 是全局变量关键字 @@num_of_instanc ...
- E20180410-hm
preface n. 序言,引语; 开端,前奏; [宗] (弥撒的) 序诵,序祷; vi. 作序; 作为…的序言,作为…的开端; 给…作序; 开始,导致; continue vi. 持 ...
- Tenka1 Programmer Beginner Contest D - IntegerotS(位运算)
传送门 题意 给出N,K,给出N对数a[i],b[i],选择一些数使得or和小于k且\(max\sum b[i]\) 分析 枚举k的每一个1位,将其删去并让低位全为1,对于每一个这样的数c,如果a[i ...
- hdoj5115【区间DP·基础】
题意: 有n头wolf排成一排,杀一头wolf回受到受到的伤害=它的本身a[i]+相邻两个b[i-1]+b[i+1].然后杀死第k个位置的wolf的话,k-1和k+1默认相邻(满足的话). 思路: 用 ...
- qq教xixi写模拟加法【非常爆炸】
#include<iostream> #include<cstdio> #include<math.h> #include<queue> #includ ...
- 基于FBX SDK的FBX模型解析与加载 -(二)
http://blog.csdn.net/bugrunner/article/details/7211515 5. 加载材质 Material是一个模型渲染时必不可少的部分,当然,这些信息也被存到了F ...
- 鸟哥私房菜基础篇:Linux 磁碟与档案系统管理习题
猫宁!!! 参考链接:http://linux.vbird.org/linux_basic/0230filesystem.php 鸟哥是为中国信息技术发展做出巨大贡献的人. 1-我们常常说,开机的时候 ...