10. Regular Expression Matching[H]正则表达式匹配
题目
Given an input string(s) and a pattern(p), 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).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters . or * .
Example1:
Input:s = "aa" p="a"
Output:false
Explanation:"a" does not match the entire string "a"
Example2:
Input:s = "aa" p="a*"
Output:true
Explanation:".*" means zero or more of the precedeng element, 'a'. Therefore, by repeating 'a' once, it become "a".
Example3:
Input:s = "ab" p=".*"
Output:true
Explanation:"." means " zero or more () of any character (.) " .
思路
动态规划
Step1. 刻画一个最优解的结构特征
\(dp[i][j]\)表示\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-1]\)是否匹配
Step2. 递归定义最优解的值
1.\(p[j-1] == s [i-1]\),则状态保存,\(dp[i][j] = dp[i-1][j-1]\)
2.\(p[j-1] ==\) .,.与任意单个字符匹配,于是状态保存,\(dp[i][j] = dp[i-1][j-1]\)
3.$p[j-1] == $*,*只能以X*的形式才能匹配,但是由于*究竟作为几个字符匹配不确定,此时有两种情况:
- \(p[j-2] != s[i-1]\),此时\(s[0,\cdots,i-1]\)与\(p[0,\cdots,j-3]\)匹配,即\(dp[i][j] = dp[i][j-2]\)
- \(p[j-2] == s[i-1]\) 或者 $p[j-2] == $
.,此时应分为三种情况:
*作为零个字符,\(dp[i][j] = dp[i][j-2]\)
*作为一个字符,\(dp[i][j] = dp[i][j-1]\)
*作为多个字符,\(dp[i][j] = dp[i-1][j]\)
Step3. 计算最优解的值
根据状态转移表,以及递推公式,计算dp[i][j]
Tips
数组初始化(python)
(1)相同的值初始化(一维数组)
#方法一:list1 = [a a a ]
list1 = [ a for i in range(3)]
#方法二:
list1 = [a] * 3
(2)二维数组初始化
初始化一个\(4*3\)每项固定为0的数组
list2 = [ [0 for i in range(3)] for j in range(4)]
C++
class Solution {
public:
bool isMatch(string s, string p) {
int m = s.length(),n = p.length();
bool dp[m+1][n+1];
dp[0][0] = true;
//初始化第0行,除了[0][0]全为false,因为空串p只能匹配空串,其他都无能匹配
for (int i = 1; i <= m; i++)
dp[i][0] = false;
//初始化第0列,只有X*能匹配空串
for (int j = 1; j <= n; j++)
dp[0][j] = j > 1 && '*' == p[j - 1] && dp[0][j - 2];
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[j - 1] == '*')
{
dp[i][j] = dp[i][j - 2] || (s[i - 1] == p[j - 2] || p[j - 2] == '.') && dp[i - 1][j];
}
else //只有当前字符完全匹配,才能传递dp[i-1][j-1] 值
{
dp[i][j] = (p[j - 1] == '.' || s[i - 1] == p[j - 1]) && dp[i - 1][j - 1];
}
}
}
return dp[m][n];
}
};
Python
def isMatch(self, s, p):
"""
:type s: str
:type p: str
:rtype: bool
"""
len_s = len(s)
len_p = len(p)
dp = [[False for i in range(len_p+1)]for j in range(len_s+1)]
dp[0][0] = True
for i in range(1, len_p + 1):
dp [0][i] = i>1 and dp[0][i - 2] and p[i-1] == '*'
for i in range (1, len_s + 1 ):
for j in range(1, len_p + 1):
if p[j - 1] == '*':
#状态保留
dp[i][j] = dp[i][j -2] or (s[i-1] == p[j-2] or p[j-2] == '.') and dp[i-1][j]
else:
dp[i][j] = (p[j-1] == '.' or s[i-1] == p[j-1]) and dp[i-1][j-1]
return dp[len_s][len_p]
10. Regular Expression Matching[H]正则表达式匹配的更多相关文章
- LeetCode OJ:Regular Expression Matching(正则表达式匹配)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- Regular Expression Matching,regex,正则表达式匹配,利用动态规划
问题描述:Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- 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 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 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode]10. Regular Expression Matching正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- 10. Regular Expression Matching正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
随机推荐
- C++ 类型转换操作与操作符重载 operator type() 与 type operator()
类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转变为其他类型值的转换.转换操作符在类定义体内声明,在保留字 operator 之后跟着转换的 ...
- mac安装python3 pandas tushare
1,升级pip python3 -m pip install --upgrade pip 2,安装依赖包 pip install --user numpy scipy jupyter pandas s ...
- jQuery中样式和属性模块简单分析
1.行内样式操作 目标:扩展框架实现行内样式的增删改查 1.1 创建 css 方法 目标:实现单个样式或者多个样式的操作 1.1.1 css方法 -获取样式 注意:使用 style 属性只能获取行内样 ...
- Android App退出检测
app的退出检测是很难的,但是获取app“要退出”的状态就容易多了,退出的瞬间并不是真的退出了,ActivityManager要销毁activity,也需要一些时间和资源的. 先见下面的运行效果: ...
- 获取json的节点名称
好几次想取json的节点名称,今天搞定了. procedure GetJsonNames(o: ISuperObject; Strs: TStrings); var ite: TSuperAvlIte ...
- PHP在Linux的Apache环境下乱码解决方法
在windows平台编写的php程序默认编码是gb2312 而linux和apche默认的编码都是UTF-8 所以windows平台编写的php程序传到linux后,浏览网页中文都是乱码. 如果手工将 ...
- TF基础5
卷积神经网络CNN 卷积神经网络的权值共享的网络结构显著降低了模型的复杂度,减少了权值的数量. 神经网络的基本组成包括输入层.隐藏层和输出层. 卷积神经网络的特点在于隐藏层分为卷积层和池化层. pad ...
- 安装oracle执行runInstaller文件时报错:“……/install/.oui:Permission denied”
一:问题描述 二:出错原因 将windows下未解压的Oracle安装软件上传到了linux服务器,导致有三个文件的执行权限丢失. 三:解决方法 为其赋予相应权限即可. 1: [root@MyPc ~ ...
- A*寻路算法详解
以我个人的理解: A*寻路算法是一种启发式算法,算法的核心是三个变量f,g,h的计算.g表示 从起点 沿正在搜索的路径 到 当前点的距离,h表示从当前点到终点的距离,而f=g+h,所以f越小,则经过当 ...
- 洛谷P1914 小书童——密码
题目背景 某蒟蒻迷上了"小书童",有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且密码是由 ...