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 ...
随机推荐
- vue项目中的路径别名
每次写引入组件的路径,如果路径嵌套比较深,那么会比较麻烦,我们可以在webpack.base.conf.js,中设置路径的别名,默认webpack设置src的别名为@ 建议配置src下一级目录的别名, ...
- npm 脚本
查看安装的包: npm list -g --depth 0 考虑到用CLI这种方式来运行本地的webpack不是特别方便,我们可以设置一个快捷方式,在package.json添加一个npm脚本(npm ...
- 005--linux基础一作业
1.最小化安装centos7操作系统 2.新增硬盘分两个分区,两个分区的文件系统依次为ext4和xfs 3.将两个文件系统分别挂在到/mnt和/opt目录 4.新增普通用户egon并采用无需重复确认的 ...
- API网关——Kong实践分享
概述 01 什么是Kong Kong是一个在Nginx中运行的Lua应用程序,可以通过lua-nginx模块实现,Kong不是用这个模块编译Nginx,而是与OpenRestry一起发布,OpenRe ...
- poj1312dfs基础
就是很简单的DFS-因为数据偏小,上去就是干了 #include <stdio.h> #include <string.h> #include <math.h> # ...
- 鸟哥私房菜基础篇:学习 Shell Scripts习题
猫宁!!! 参考链接:http://cn.linux.vbird.org/linux_basic/0340bashshell-scripts.php 鸟哥是为中国信息技术发展做出巨大贡献的人. 1-请 ...
- Mantis优化改造(技术篇)
为什么要写这篇? 既然都过去这么久了,都回忆不起来了,为什么还要整理出来这篇文章呢? 这还要追溯到2018年3月份. 当时换工作,面试了国内某知名电视厂商. 简历上面写了我优化改造了bug管理系统ma ...
- the little schemer 笔记(4)
第四章 numbers games 14 是原子吗 是的,数都是原子 (atom? n) 是真还是假,其中n是14 真,14 是原子 -3是数吗 是的,不过我们暂不考虑负数 3.14159是数吗 是的 ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- Painful Bases LightOJ - 1021
Painful Bases LightOJ - 1021 题意:给出0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F中的一些字符(不重复)还有一个进制base,求这些字符的排列形成的ba ...