‘.’ 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

多阶段决策过程的最优化问题

子问题的重叠性

p[i][j]  表示字串 s[i...len(s)], p[j...len(p)] 是否可以匹配。

递归地定义最优值

如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;

如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。

匹配成功的条件为S[i]=='\0' && P[j]=='\0'。

dp[i][j] = c1. p[j+1] != *.   if s[i] == p[j]  dp[i][j] = dp[i+1][j+1]       else dp[i][j] = false

c2 p[j+1] == '*'         if( s[i] ==  p[j] || p[j] == '.' && (*s) != 0)

p[j] == .  因为他可以匹配任何字符,所以和相等关系有基本一样的方式。

(1)p[j+1]不是'*'。情况比较简单,只要判断如果当前s的i和p的j上的字符一样(如果有p在j上的字符是'.',也是相同),并且res[i][j]==true,则res[i+1][j+1]也为true,res[i+1][j+1]=false; 
(2)p[j+1]是'*',但是p[j]!='.'。那么只要以下条件有一个满足即可对res[i+1][j+1]赋值为true: 
    1)res[i+1][j]为真('*'只取前面字符一次); 
    2)res[i+1][j-1]为真('*'前面字符一次都不取,也就是忽略这两个字符); 
    3)res[i][j+1] && s[i]==s[i-1] && s[i-1]==p[j-1](这种情况是相当于i从0到s.length()扫过来,如果p[j+1]对应的字符是‘*’那就意味着接下来的串就可以依次匹配下来,如果下面的字符一直重复,并且就是‘*’前面的那个字符)。 
(3)p[j+1]是'*',并且p[j]=='.'。因为".*"可以匹配任意字符串,所以在前面的res[i+1][j-1]或者res[i+1][j]中只要有i+1是true,那么剩下的res[i+1][j+1],res[i+2][j+1],...,res[s.length()][j+1]就都是true了。 
这道题有个很重要的点,就是实现的时候外层循环应该是p,然后待匹配串s内层循环扫过来

res[][] 保持中间计算值

/** regular.cpp**  Created on: 2014年12月26日*      Author: judyge*/
#include <iostream>
#include <string>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
using namespace std;bool isMatch(string s, string p) {if(s.length()==0 && p.length()==0)return true;if(p.length()==0)return false;bool res[s.length()+1][p.length()+1];res[0][0] = true;for(int j=0;j<p.length();j++){if(p[j]=='*'){if(j>0 && res[0][j-1]) res[0][j+1]=true;if(j<1) continue;if(p[j-1]!='.'){for(int i=0;i<s.length();i++){if(res[i+1][j] || j>0&&res[i+1][j-1]|| i>0 && j>0 && res[i][j+1]&&s[i]==s[i-1]&&s[i-1]==p[j-1])res[i+1][j+1] = true;}}else{int i=0;while(j>0 && i<s.length() && !res[i+1][j-1] && !res[i+1][j])i++;for(;i<s.length();i++){res[i+1][j+1] = true;}}}else{for(int i=0;i<s.length();i++){if(s[i]==p[j] || p[j]=='.')res[i+1][j+1] = res[i][j];}}}return res[s.length()][p.length()];
}int main(){char *s="aab";char *p="c*a*b";char *m="aaa";char *n="aa";clock_t start,finish;double time;start=clock();cout<<isMatch(s,p)<<"\n";cout<<isMatch(m,n)<<"\n";finish=clock();time=(double)((finish-start)/CLOCKS_PER_SEC);printf("start:%ld\t\tfinish:%ld\tfinish-start:%ld\truntime:%f\n",start,finish,finish-start,time);return 0;}

运行结果

1
0
start:1     finish:1    finish-start:0  runtime:0.000000

LeetCode Regular Expression Matching相关推荐

  1. LeetCode Regular Expression Matching(.和*通配符匹配)

    给出text和pattern,pattern中可以使用通配符.和*,.表示匹配任意字符,*表示匹配上一个字符 从text和pattern的第一个字符开始做匹配 1.第一个字符匹配成功,要么第一个字符相 ...

  2. leetcode 10 Regular Expression Matching

    题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...

  3. 【重点 递归 动态规划 正则表达式匹配】LeetCode 10. Regular Expression Matching

    LeetCode 10. Regular Expression Matching 本博客参考:http://www.cnblogs.com/grandyang/p/4461713.html 详细解析见 ...

  4. 【LeetCode】10. Regular Expression Matching

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  5. LeetCode 10. Regular Expression Matching / 44. Wildcard Matching

    10. Regular Expression Matching 经典DP题目,比较复杂,需要多复习. dp[i][j] 表示 s 下标0~i,p 下标0~j 是否能够匹配   dp[i-1][j-1] ...

  6. Leetcode Q10: Regular Expression Matching

    题目10: (该题目拿到手没什么特别好的思路,从网上看的别人的解法,然后写了下自己的理解,需要常回顾) Implement regular expression matching with suppo ...

  7. Q10 Regular Expression Matching

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

  8. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

  9. 【算法】Regular Expression Matching 正则匹配

    [算法]Regular Expression Matching 正则匹配 题目 解题思路 代码实现 题目 Given an input string ( s ) and a pattern ( p ) ...

最新文章

  1. 54页PPT揭示AI革命及其前沿进展!
  2. Vertical Menu ver4
  3. 2019-05-30启动redis 后台服务运行·
  4. ASP.NET Core 介绍和项目解读
  5. qt中设置父窗口中某一控件样式后,子窗口中某一控件也随父控件样式
  6. Python计算从n个元素中任选i个的组合数C(n,i)
  7. 告别鼠标——【Windows下常见系统快捷键】
  8. SQL基础---SQL DELETE 语句
  9. 《流言终结者》,再见
  10. http服务器和application服务器区别
  11. SAP License:ERP系统操作详解
  12. 怎么把kux格式转换成mp4?完美转换优酷kux格式
  13. SaliencyReview:显著性检测综述阅读笔记
  14. 2019胡润全球富豪榜:北京成为世界10亿美元富豪之都
  15. Git上传文件时报错:The authenticity of host xxx can‘t be established.
  16. 实现AI角色的自主移动——操控行为
  17. Ebook管理工具(持续更新)
  18. 查询没学过“张三“老师讲授的任一门课程的学生姓名
  19. 2021高考枣庄八中成绩查询,2021年枣庄高考状元是谁分数多少分,历年枣庄高考状元名单...
  20. StarFS方案 | 并行存储加速影视动漫渲染

热门文章

  1. 变参标准函数的重新封装,如printf
  2. [SQL] 外卖系统数据库设计
  3. 51Nod 1453 抽彩球
  4. Android 利用an框架快速实现网络请求(含下载上传文件)
  5. selectpselect/pollppoll/epoll
  6. asp.net(C#)套用模板操作Excel。
  7. 后台返回给前端数据拆分成三级菜单
  8. Spring Boot集成Hazelcast实现集群与分布式内存缓存
  9. Linux服务器下的HTTP抓包分析
  10. 通知栏管理NotificationListenerService