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 1.
bool isMatch(string s, string p) {
int ls = s.length(), lp = p.length(), i, j;
vector<vector<int>> dp(, vector<int>(lp + , ));
bool k = ;
dp[][] = ; dp[][] = ;
for (i = ; i <= lp; i++)
dp[][i] = (dp[][i - ] && (p[i - ] == '*'));
for (i = ; i <= ls; i++)
{
dp[k][] = ;
for (j = ; j <= lp; j++)
{
if('*' == p[j-] && j > )
{
if(p[j-] == s[i-] || '.' == p[j-])
dp[k][j] = dp[k][j-] | dp[!k][j];
else
dp[k][j] = dp[k][j-];
}
else if(p[j-] == s[i-] || '.' == p[j-])
dp[k][j] = dp[!k][j-];
else
dp[k][j] = ;
}
k = !k;
}
return dp[!k][lp];
}

2.

bool isMatch(string s, string p) {
int ls = s.length(), lp = p.length(), i, j;
if(p == "")
return s == "";
if( == lp || p[] != '*')
{
if(s == "" || (p[] != '.' && s[] != p[]))
return false;
return isMatch(s.substr(), p.substr());
}
//p[1] == '*'
for(i = -; i < ls && (- == i || '.' == p[] || s[i] == p[]); i++ )
{
if(isMatch(s.substr(i+), p.substr()))
return true;
}
}

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. swagger报错No handler found for GET /swagger-ui.html

    今天下载jeeweb框架下来研究,其他还有,就是swagger老是出不来.报错:No handler found for GET /swagger-ui.html 后来搜索才发现,这个错误,是因为资源 ...

  2. Django 将数据库查出的 QuerySet 对象转换为 json 字符串

    通过Django查询出MySQL数据库的数据,并将查询出的QuerySet 对象转化成 json 字符串. 写这个例子的作用主要是用来为手机端提供接口用,记录一下,以后 说不准 肯定能用到! ---- ...

  3. tensorflow reduction_indices理解

    在tensorflow的使用中,经常会使用tf.reduce_mean,tf.reduce_sum等函数,在函数中,有一个reduction_indices参数,表示函数的处理维度,直接上图,一目了然 ...

  4. Python3基础 str find+index 是否存在指定字符串,有则返回第一个索引值

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. linux性能分析工具之火焰图

    一.环境 1.1 jello@jello:~$ uname -a Linux jello 4.4.0-98-generic #121-Ubuntu SMP Tue Oct 10 14:24:03 UT ...

  6. Linq join right join left join

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  7. JavaScript 装饰者模式(this运用)

    例: function ConcreteClass() { this.performTask = function () { this.preTask(); console.log('doing so ...

  8. Ubuntu Eclipse ns3编译中 遇到的OSError 系列问题

    问题1:Permission denied 解决方法:修改文件权限,利用 chmod 命令 修改在 /home/wasdns/workspace/MyNS3_Mac/ns-3.25 (eclipse工 ...

  9. Redis集群学习笔记

    Redis集群学习笔记 前言 最近有个需求,就是将一个Redis集群中数据转移到某个单机Redis上. 迁移Redis数据的话,如果是单机Redis,有两种方式: a. 执行redis-cli shu ...

  10. 一个对iBatis的总结写的不错(转载)

    转载自:http://blog.csdn.net/panxueji/article/details/9852795 一. ibatis介绍 ibatis始于2002年,2010年更名为mybatis, ...