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. kvm的分层控制

    第五层 virsh virt-manager(和libvirtd)利用了libvirt-api   virsh pool-list --all   virsh pool-define xxx/xml会 ...

  2. NGINX学习积累(学习牛人)

    大牛:http://www.cnblogs.com/zengkefu/p/5563608.html 当请求来临的时候,NGINX会选择进入虚拟主机,匹配location后,进入请求处理阶段. 在请求处 ...

  3. 吴裕雄--天生自然ORACLE数据库学习笔记:PL/SQL编程

    set serveroutput on declare a ; b ; c number; begin c:=(a+b)/(a-b); dbms_output.put_line(c); excepti ...

  4. Kafka-JavaAPI(Producer And Consumer)

    Kafka--JAVA API(Producer和Consumer) Kafka 版本2.11-0.9.0.0 producer package com.yzy.spark.kafka; import ...

  5. Python流程控制-3 循环控制

    循环控制,就是让程序循环运行某一段代码直到满足退出的条件,才退出循环. Python用关键字for和while来进行循环控制,但是没有其它语言的do...while语句(在Java和PHP中都有do ...

  6. Java-用星号打印菱形

    打印如图所示菱形9行9列(提示可以将菱形分成上下两个三角形,分析每行空格数和星号个数的关系) 代码如下: package com.homework.lhh; public class Ex20 { p ...

  7. Codeforces1301C. Ayoub's function

    本题的收获是,要学会反向思维,正向找包含1的太多,我们就反向找,全排列-只有0的不满足题意的就是答案,一共有n-m个0,m个1,插空法,一共有m+1个地方可以插入0序列,总排列数为(n+1)*n/2, ...

  8. luogu P4013 数字梯形问题

    三倍经验,三个条件,分别对应了常见的3种模型,第一种是限制每个点只能一次且无交点,我们可以把这个点拆成一个出点一个入点,capacity为1,这样就限制了只选择一次,第二种是可以有交点,但不能有交边, ...

  9. Docker 问题[Warning] IPv4 forwarding is disabled. Networking will not work.

    Docker 问题[Warning] IPv4 forwarding is disabled. Networking will not work. 在使用Dockerfile创建Docker镜像的时候 ...

  10. php 增删改查范例(2)

    增加页面add.php: <!DOCTYPE html><html lang="en"><head>    <meta charset=& ...