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 ...
随机推荐
- Oracle的db.properties文件
转自:https://blog.csdn.net/lssqk/article/details/79133829
- Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mao.PersonSet
转自:https://blog.csdn.net/vipmao/article/details/51334743
- windows server安装zabbix-agent
1.准备安装包: 下载链接:https://www.zabbix.com/downloads/3.4.0/zabbix_agents_3.4.0.win.zip 2.在C盘下创建一个zabbix目录, ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 06. Controller 返回View
Controller父类会提供很多上下文的相关信息,还提供了很多封装的方法 返回的对象要求实现了IActionResult接口 继承父类,并引入命名空间 写this点就出现很多东西,这些就是上下文的信 ...
- GitHub笔记---邮箱访问错误
GitHub地址太长,所以需要一个变量来保存 把远程仓库赋值给一个变量,以后就用就这变量代表这个地址 GitHub推送push 推送过程中发生一个小插曲,出现了错误,错误提示我复制过来吧 remote ...
- 无向图的边双连通分量(EBC)
嗯,首先边双连通分量(双连通分量之一)是:在一个无向图中,去掉任意的一条边都不会改变此图的连通性,即不存在桥(连通两个边双连通分量的边),称作边双连通分量.一个无向图的每一个极大边双连通子图称作此无向 ...
- Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row 1
前端插入数据的时候报如下错误: Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row ...
- UWP 页面跳转传值
如果涉及到页面跳转,一般用Frame这个控件来管理不同的页面. <Grid Name="RootGrid"> <Frame Name="RootFram ...
- HDU - 6063 RXD and math
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6063 打表发现规律是n^k #include <iostream> #inc ...
- 优先队列 HDOJ 5437 Alisha's Party
题目传送门 题意:一人过生日,很多人排着队送礼物.排队顺序是礼物价值大的优先,如果相等先来的优先.有m次开门,当t个人到了会开门让p个人进门.最后一次让门外的所有人按顺序进门.有q次询问,问第x个进门 ...