10. Regular Expression Matching *HARD*
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 1.
bool isMatch(string s, string p) {
int ls = s.length(), lp = p.length(), i, j;
vector<vector<int>> dp(, vector<int>(lp + , ));
bool k = ;
dp[][] = ; dp[][] = ;
for (i = ; i <= lp; i++)
dp[][i] = (dp[][i - ] && (p[i - ] == '*'));
for (i = ; i <= ls; i++)
{
dp[k][] = ;
for (j = ; j <= lp; j++)
{
if('*' == p[j-] && j > )
{
if(p[j-] == s[i-] || '.' == p[j-])
dp[k][j] = dp[k][j-] | dp[!k][j];
else
dp[k][j] = dp[k][j-];
}
else if(p[j-] == s[i-] || '.' == p[j-])
dp[k][j] = dp[!k][j-];
else
dp[k][j] = ;
}
k = !k;
}
return dp[!k][lp];
}
2.
bool isMatch(string s, string p) {
int ls = s.length(), lp = p.length(), i, j;
if(p == "")
return s == "";
if( == lp || p[] != '*')
{
if(s == "" || (p[] != '.' && s[] != p[]))
return false;
return isMatch(s.substr(), p.substr());
}
//p[1] == '*'
for(i = -; i < ls && (- == i || '.' == p[] || s[i] == p[]); i++ )
{
if(isMatch(s.substr(i+), p.substr()))
return true;
}
}
10. Regular Expression Matching *HARD*的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- pyDay7
内容来自廖雪峰的官方网站 1.如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 2.只要是可迭代对象,无论有无下标, ...
- 20145302张薇 《网络对抗》MSF应用基础
20145302张薇 <网络对抗>MSF应用基础 实验内容 掌握metasploit的基本应用方式 1.主动攻击--ms08_067 2.针对浏览器的攻击--ms11_050 3.针对客户 ...
- 在Android Studio中创建项目和模拟器
北京电子科技学院 实 验 报 告 课程:移动平台应用开发实践 班级:201592 姓名:杨凤 学号:20159213 成绩:___________ 指导老师:娄嘉 ...
- Beetl模板引擎入门教程
最近项目中有个邮件发送的需求,不过要求发送的HTML格式的邮件.由于Beetl对java语言的良好支持和很好的性能,我们决定使用Beetl作为我们的模板引擎. Beetl官网已经有了很详细的教程,所以 ...
- NOIP Mayan游戏 - 搜索
Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个7行5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定的步数内消除所有 ...
- UVa 437 巴比伦塔
https://vjudge.net/problem/UVA-437 这道题和HDU的Monkey and Banana完全一样. #include<iostream> #include& ...
- python学习——大文件分割与合并
在平常的生活中,我们会遇到下面这样的情况: 你下载了一个比较大型的游戏(假设有10G),现在想跟你的同学一起玩,你需要把这个游戏拷贝给他. 然后现在有一个问题是文件太大(我们不考虑你有移动硬盘什么的情 ...
- spring boot开发 @autowired注入失败
@autowired注入失败 会出现如下错误提示: 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...
- docker相关杂项
代理 在vscode里build image习惯了,但是今天 从docker hub上pull python镜像,最后一个层,始终是waiting状态,pull不下来 好像不能临时.只能设置 http ...
- Bootstrap 栅格系统(布局)
基本用法 网格系统用来布局,其实就是列的组合.Bootstrap框架的网格系统中有四种基本的用法.由于Bootstrap框架在不同屏幕尺寸使用了不同的网格样式,在这一节中所涉及到的示例,我们都以中屏 ...