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 ...
随机推荐
- javascript学习中自己对作用域和作用域链理解
在javascript学习中作用域和作用域链还是相对难理解些,下面我关于javascript作用域和作用域链做一下详细介绍,给各位初学者答疑解惑. 首先我们介绍一下什么是作用域? 从字面上理解就是起 ...
- Android学习——碎片Fragment的使用
一.碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间) 1.新建一个FragmentTest项目: 新建一个左侧碎片布局left_fragment.xml,代码如下:(只放置一 ...
- 最影响APP软件质量和成本的三个方面。希望大家一定要记在心里!
1.功能的开发方式 现在市场上存在的几种开发方式如下: a.web网页加壳生成APP web网页加壳生成APP的开发方式,先花几百块钱买个现成的手机网站模板,在加壳打包一个APP只需要5分钟,但是做出 ...
- Mysql优化-为表字段添加索引
1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTE ...
- MaterialDesign动画
一.概述 MaterialDesign设计理念 MaterialDesign动画 二.实例讲解 (1)Touch Feedback (2)Reveal Effect (3)Activity Trans ...
- 4056 hdu4866 Shooting
题目描述 In the shooting game, the player can choose to stand in the position of [1, X] to shoot, you ca ...
- Python学习笔记(3)for循环和while循环
2019-02-25 (1)break语句:终止当前循环,跳出循环体. (2)continue语句:终止本轮循环并开始下一轮循环(在下一轮循环开始前,会先测试循环条件). (3)for循环 ① ran ...
- 【Codeforces Round #493 (Div. 2) B】Cutting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有在前i个位置奇数偶数出现次数都相同的地方才能切. (且不管前面怎么切,这里都能切的. 那么就相当于有n个物品,每个物品的代价 ...
- 【codeforces 724E】Goods transportation
[题目链接]:http://codeforces.com/problemset/problem/724/E [题意] 有n个城市; 这个些城市每个城市有pi单位的物品; 然后已知每个城市能卖掉si单位 ...
- C++ "#"的作用和用法
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/48879093 1 #和##的作用和用法 ...