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
@首先题目要理解,通配符*是重复前面一个元素,而不是*前面所有的元素而且通配符*号前面必须要有元素,就是说*出现的位置不可能在第一位。
f[i][j] = f[i][j - ] || (s[i - ] == p[j - ] || '.' == p[j - ]) && f[i - ][j];

f[i][j - 2]表示前面的元素出现0次,后面表示出现次数大于等于1.

aabbb

aab.*

能够出现多次,说明s中减少一个(i -1)也能匹配,所以这个条件也必须满足。

s[i - 1] == p[j - 2]因为ij表示出现的元素个数,相当于下标从i - 1,j - 1.
表示p中倒数第二个元素要和s中倒数第一个元素相等。这样才能进行重复。
注意初始化第一列的情况。
class Solution {
public:
bool isMatch(string s, string p) {
if(s.size() == && p.size() == ){
return true;
}
int m = s.size();
int n = p.size();
vector<vector<bool>> dp(m + ,vector<bool> (n + ,false)); dp[][] = true;
for(int i = ;i <= m;++i){
dp[i][] = false;
}
for(int j = ;j <= n;++j){
if((j > ) && (j % == ) && dp[][j - ] && p[j - ] == '*'){
dp[][j] = true;
}
} for(int i = ;i <= m;++i){
for(int j = ;j<= n;++j){
if(p[j - ] != '*'){
dp[i][j] = dp[i - ][j - ] && (s[i - ] == p[j - ] || '.' == p[j - ]);
}
else{
dp[i][j] = dp[i][j - ] || dp[i - ][j] && ((s[i - ] == p[j - ]) || '.' == p[j - ]);
}
}
}
return dp[m][n];
}
};

10. Regular Expression Matching正则表达式匹配的更多相关文章

  1. [LeetCode] 10. Regular Expression Matching 正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  2. [LeetCode]10. Regular Expression Matching正则表达式匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for ...

  3. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  4. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  5. 10. Regular Expression Matching字符串.*匹配

    [抄题]: Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  6. 010 Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'.'.' Matches any single character. ...

  7. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  8. Leetcode 10. Regular Expression Matching(递归,dp)

    10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...

  9. leetcode 10. Regular Expression Matching 、44. Wildcard Matching

    10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...

随机推荐

  1. 操作系统OS - 线程中的join()为什么叫join

    1. 问题:很好奇为什么叫Join? 参考: https://blog.csdn.net/frankarmstrong/article/details/55504161 https://stackov ...

  2. Inject shellcode into PE file

    先声明这是不免杀的,只是演示. 哔哩哔哩视频 新增节 一般能实现特定功能的shellcode的长度都比较长,可以分到几个节上的空白区,但是这样麻烦啊,或者把最后一个节扩大,但是最后一个节一般没有执行的 ...

  3. vim功能之替换和查找

    vim有着强大的替换和查找功能,若能进行熟练的运用,可以让工作效率得到一个很大程度的提高. 替换 语法:[addr]s/源字符串/目的字符串/[option] [addr]表示检索范围,如: &quo ...

  4. HDU1176免费馅饼(DP)

    都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内.馅饼如果掉在了地上当然就 ...

  5. IELTS Writing Task 1: two-chart answer

    Thursday, January 09, 2020 The chart below shows the value of one country's exports in various categ ...

  6. 树莓派4B踩坑指南 - (2)安装系统及初始化

    安装系统及初始化 格式化TF卡:SDFormatter 4.0.如果需要换系统,则必须先烧录进一个空img,然后再格式化! 烧录系统:Win32DiskImager-0.9.5 更改默认密码:账号pi ...

  7. Java如何实现序列化,有什么意义?

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States, 但是Java给你提供一种应该 ...

  8. Java中使用JSONTokener判断接口返回字符串是JSONObject还是JSONArray

    今天在接口对接中,遇到一个问题,对方接口返回的JSONString,类型不确定,所以需要先做判断再进行处理.查阅资料后使用JSONTokener可进行处理,特此记录. String ret = ord ...

  9. Maven项目-Tomcat - 方法无法为jsp编译类ClassFormatException的解决

    解决方法:

  10. Spring MVC中的ResponseEntity和ResponseBody的区别

    1.ResponseEntity的优先级高于@ResponseBody. 在不是ResponseEntity的情况下才去检查有没有@ResponseBody注解. 如果响应类型是ResponseEnt ...