Implement regular expression matching with support for '.' and '*'.

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "a*") true
  14. isMatch("aa", ".*") true
  15. isMatch("ab", ".*") true
  16. isMatch("aab", "c*a*b") true
  17.  
  18. 1.
  1. bool isMatch(string s, string p) {
  2. int ls = s.length(), lp = p.length(), i, j;
  3. vector<vector<int>> dp(, vector<int>(lp + , ));
  4. bool k = ;
  5. dp[][] = ; dp[][] = ;
  6. for (i = ; i <= lp; i++)
  7. dp[][i] = (dp[][i - ] && (p[i - ] == '*'));
  8. for (i = ; i <= ls; i++)
  9. {
  10. dp[k][] = ;
  11. for (j = ; j <= lp; j++)
  12. {
  13. if('*' == p[j-] && j > )
  14. {
  15. if(p[j-] == s[i-] || '.' == p[j-])
  16. dp[k][j] = dp[k][j-] | dp[!k][j];
  17. else
  18. dp[k][j] = dp[k][j-];
  19. }
  20. else if(p[j-] == s[i-] || '.' == p[j-])
  21. dp[k][j] = dp[!k][j-];
  22. else
  23. dp[k][j] = ;
  24. }
  25. k = !k;
  26. }
  27. return dp[!k][lp];
  28. }

2.

  1. bool isMatch(string s, string p) {
  2. int ls = s.length(), lp = p.length(), i, j;
  3. if(p == "")
  4. return s == "";
  5. if( == lp || p[] != '*')
  6. {
  7. if(s == "" || (p[] != '.' && s[] != p[]))
  8. return false;
  9. return isMatch(s.substr(), p.substr());
  10. }
  11. //p[1] == '*'
  12. for(i = -; i < ls && (- == i || '.' == p[] || s[i] == p[]); i++ )
  13. {
  14. if(isMatch(s.substr(i+), p.substr()))
  15. return true;
  16. }
  17. }

10. Regular Expression Matching *HARD*的更多相关文章

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

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

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

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

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

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

  4. 刷题10. Regular Expression Matching

    一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...

  5. leetcode problem 10 Regular Expression Matching(动态规划)

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

  6. 10. Regular Expression Matching

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

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

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

  8. [LeetCode] 10. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...

  9. 【leetcode】10.Regular Expression Matching

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

  10. Java [leetcode 10] Regular Expression Matching

    问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...

随机推荐

  1. P2503 [HAOI2006]均分数据

    P2503 [HAOI2006]均分数据 模拟退火+dp (不得不说,我今天欧气爆棚) 随机出1个数列,然后跑一遍dp统计 #include<iostream> #include<c ...

  2. tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南(含nginx/https支持)

    之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...

  3. CF#235E. Number Challenge

    传送门 可以理解为上一道题的扩展板.. 然后我们就可以YY出这样一个式子 ${\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^cd(ijk)=\sum_{i=1}^a\sum_{ ...

  4. 20145329 《网络对抗技术》MSF基础应用

    实践目标 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.具体需要完成(1)一个主动攻击,如ms08_067;(2)一个针对浏览器的攻击,如ms11_050:(3)一个针对客户端 ...

  5. 调试工具--console用法收藏

    1.使用console进行性能测试和计算代码运行时间:http://www.cnblogs.com/0603ljx/p/4387628.html 2.console命令详解:http://www.cn ...

  6. luogu P3387 【模板】缩点

    题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...

  7. springmvc-自定义消息转换器

    最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧! 如有不对地方望指出! 一.自定义类实现AbstractHttpMessageConverter package com.dz ...

  8. 【安装】Microsoft SQL Server的安装

    数据库版本:2012 系统环境:windows 7 一.安装 依次选择“安装->全新 SQL Server 独立安装或向现有安装添加功能”;点“确定” 选择版本,推荐标准版,这里是直接输入序列号 ...

  9. label表单的关联性

    <input type="checkbox" id="cr" /> <label for="cr">点击关联复选框& ...

  10. Ubuntu16.04 无法连接WiFi

    在安装完 ns-3.25 之后,着手开始准备 Eclipse 的安装,打开了 Firefox游览器 准备上网的时候,发现网络没有正常连接. 刚刚开始怀疑的是,并没有连接上网络. 于是打开了终端,pin ...