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 ...
随机推荐
- mysql与oracle常用函数及数据类型对比00持续补充
最近在转一个原来使用oracle,改为mysql的系统,有些常用的oracle函数的mysql实现顺便整理了下,主要是系统中涉及到的(其实原来是专门整理过一个详细doc的,只是每次找word麻烦). ...
- 利用脚本kill掉进程, 语法:运行脚本+进程名
下面附上脚本, 权限需要附X执行 #!/bin/sh #pid kill thread for chenglee #if fileformat=dos, update fileformat=unix ...
- this逃逸
首先,什么是this逃逸? this逃逸是指类构造函数在返回实例之前,线程便持有该对象的引用. 常发生于在构造函数中启动线程或注册监听器. eg: public class ThisEscape { ...
- 20145106 《Java程序设计》第10周学习总结
教材学习内容总结 什么是计算机网络? 计算机网络,是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享 ...
- Win32 消息响应顺序
如果窗口处理函数响应了 WM_RBUTTONUP后,不会响应WM_CONTEXTMENU消息了.
- ubuntu16.04下内核模块解析
一.环境如下: 1.1内核版本: jello@jello:~$ uname -a Linux jello 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19: ...
- SQL NULL
表 select CHARACTER_MAXIMUM_LENGTH from information_schema.columns where table_name= 'Alliance' selec ...
- 格子中输出|2015年蓝桥杯B组题解析第四题-fishers
StringInGrid函数会在一个指定大小的格子中打印指定的字符串. 要求字符串在水平.垂直两个方向上都居中. 如果字符串太长,就截断. 如果不能恰好居中,可以稍稍偏左或者偏上一点. 下面的程序实现 ...
- 用 SwitchHosts设置hotst, 用法示例
涉及到本地默认ip(localhost,127.0.0.1)设置关联地址时,使用XAMPP本地服务器时避免自动跳转设置的域名的一些处理方法 打开此文件,把内容修改如下 # Virtual Hosts# ...
- JavaScript权威指南2.词法结构
字符集 1.用16位的Unicode字符集编写的,可以表示地球上通用的每一种书面语言.国际化 2.每个字符都是用两个字节表示的 3.大小写敏感:关键字.变量.函数名.标识符:HTML并不区分大小写 H ...