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. Linux 环境 HTTP 服务器

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in ...

  2. elasticsearch分词器ik

    1. 下载和es配套的版本 git clone https://github.com/medcl/elasticsearch-analysis-ik 2. 编译 cd elasticsearch-an ...

  3. 使用CloudFlare 的 PKI 工具集 cfssl 来生成 Certificate Authority (CA) 证书和秘钥文件

    要安装kubernetes最新版集群,https://github.com/opsnull/follow-me-install-kubernetes-cluster 这个文档必须要研习一下了. 以下实 ...

  4. 20145127《java程序设计》第一次实验

    <java程序设计>第一次实验 实验内容及其步骤 1.使用JDK编写简单的Java小程序: Java编译的方法有很多,最基础最简单的就是使用命令行,记事本,Java虚拟机直接进行编译,下面 ...

  5. Android实践项目汇报-改(一)

    Google天气客户端NABC Need(需求):  功能性需求分析 天气预报客户端,顾名思义就是为用户提供实时准确的天气信息,方便用户出行生活.根据用户日常需求,软件完成后点开,载入界面,显示查询界 ...

  6. STM32.printf

    printf("\r\n this is a usart printf demo \r\n"); Use Micro LIB 需要勾选这个库 将串口定义成 printf 函数 #i ...

  7. JAVA I/O(二)文件NIO

    一.Unix五种I/O模型 读取和写入文件I/O操作都是调用操作系统提高的接口,对磁盘I/O来说,一般是将数据从磁盘拷贝到内核空间,然后从内核空间拷贝到用户空间.为了减小I/O时间,一般内核空间存在高 ...

  8. goole机器学习视频链接【学习笔记】

    作者:庄泽彬 说明:在youtu上观看的google的机器学习相关的视频,如何fangqiang请自己解决 机器学习简介:https://www.youtube.com/watch?time_cont ...

  9. IntelliJ-IDEA和Git、GitHub、Gitlab的使用

    一.基本入门 1.IntelliJ-IDEA预装的版本控制介绍 我们来看IntelliJ-IDEA的版本控制设置区域 打开File>Settings>Version Control  可以 ...

  10. Unity3D学习笔记(一):Unity简介、游戏物体、组件和生命周期函数

    Project(工程.项目):工程是把游戏开发当前所需要的资源归类管理用的. Console控制台:日志.报错.调试,右上角,消息过滤 Assets:资源,存储游戏中一切用到的资源 Library:临 ...