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

首先这里有个可能大家不知道的地方:

if p[0] = '*', the string must be an invalid string.
that is what Regular Expression defined.

这是别人在评论的时候说的。也就是说,pattern的第一个不可以是*;


class Solution {
public:
bool isMatch(string s, string p) {
if (p.empty()) return s.empty(); if ('*' == p[1])
// x* matches empty string or at least one character: x* -> xx*
// *s is to ensure s is non-empty
return (isMatch(s, p.substr(2)) || !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p));
else
return !s.empty() && (s[0] == p[0] || '.' == p[0]) && isMatch(s.substr(1), p.substr(1));
}
}; class Solution {
public:
bool isMatch(string s, string p) {
/**
* f[i][j]: if s[0..i-1] matches p[0..j-1]
* if p[j - 1] != '*'
* f[i][j] = f[i - 1][j - 1] && s[i - 1] == p[j - 1]
* if p[j - 1] == '*', denote p[j - 2] with x
* f[i][j] is true iff any of the following is true
* 1) "x*" repeats 0 time and matches empty: f[i][j - 2]
* 2) "x*" repeats >= 1 times and matches "x*x": s[i - 1] == x && f[i - 1][j]
* '.' matches any single character
*/
int m = s.size(), n = p.size();
vector<vector<bool>> f(m + 1, vector<bool>(n + 1, false)); f[0][0] = true;
for (int i = 1; i <= m; i++)
f[i][0] = false;
// p[0.., j - 3, j - 2, j - 1] matches empty iff p[j - 1] is '*' and p[0..j - 3] matches empty
for (int j = 1; j <= n; j++)
f[0][j] = j > 1 && '*' == p[j - 1] && f[0][j - 2]; for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (p[j - 1] != '*')
f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1] || '.' == p[j - 1]);
else
// p[0] cannot be '*' so no need to check "j > 1" here
f[i][j] = f[i][j - 2] || (s[i - 1] == p[j - 2] || '.' == p[j - 2]) && f[i - 1][j]; return f[m][n];
}
};
 

算法分析-leedcode正则题目的更多相关文章

  1. 正则和grep——再做正则就去死

    grep 文本过滤工具 基本正则表达式 grep 语法 基本正则表达式的元字符 次数匹配 位置锚定 分组 扩展正则表达式 基本正则表达式的元字符 次数匹配 位置锚定 分组 或者 grep的介绍 lin ...

  2. hiho一下 第四十九周 题目1 : 欧拉路·一【无向图 欧拉路问题】

    题目1 : 欧拉路·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最 ...

  3. poj 1422 Air Raid 最少路径覆盖

    题目链接:http://poj.org/problem?id=1422 Consider a town where all the streets are one-way and each stree ...

  4. POJ_2769同余问题

    Reduced ID Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9310   Accepted: 374 ...

  5. RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素

    RemoveDuplicatesFromSortedArrayI: 问题描述:给定一个有序数组,去掉其中重复的元素.并返回新数组的长度.不能使用新的空间. [1,1,2,3] -> [1,2,3 ...

  6. hihocoder 微软编程之美2015 初赛 第二场(暴力+字典序+图论+思维算法)

    题目1 : 扑克牌 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 一副不含王的扑克牌由52张牌组成,由红桃.黑桃.梅花.方块4组牌组成,每组13张不同的面值.现在给定52 ...

  7. SDUT OJ I样(0-1背包问题 【模板】)

    I样 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 这是个什么问题呢?DP,贪心,数据结构,图论,数论还是计算几何?管他呢,反正 ...

  8. POJ 2309 BST(二叉搜索树)

    BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8657   Accepted: 5277 Description C ...

  9. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

随机推荐

  1. 学php之翻译wordpress(1)

    单看文件结构,wordpress分了3个文件夹 wp-admin,wp-content,wp-includes 和零散的一堆php,暂时不清楚各自的分工 入口文件是index.php <?php ...

  2. plsql基本语法(

    1. 定义常量的语法格式    常量名 constant 类型标识符 [not null]:=值;    常量,包括后面的变量名都必须以字母开头,不能有空格,不能超过30个字符长度,同时不能和保留字同 ...

  3. MySQL入门转载

    21分钟 MySQL 入门教程 http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html 目录 一.MySQL的相关概念介绍 二.Wi ...

  4. python实现简单表单校验框架

    # encoding=utf-8 from app.models import Student from flask import g import re from flask.ext.wtf imp ...

  5. Counting Triangles(hd1396)

    Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. SQL连接方式(内连接,外连接,交叉连接)

    1.内连接.左连接.右连接.全连接介绍 內连接仅选出两张表中互相匹配的记录.因此,这会导致有时我们需要的记录没有包含进来.内部连接是两个表中都必须有连接字段的对应值的记录,数据才能检索出来.   左连 ...

  7. Android-PullToRefresh下拉刷新库基本用法

    How:(使用) 转自:http://blog.csdn.net/hantangsongming/article/details/42490277 PullToRefresh是一套实现非常好的下拉刷新 ...

  8. cf466B Wonder Room

    B. Wonder Room time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. hdu4334 Trouble 合并集合可以降低复杂度阿啦啦

    我觉得这一题方法很好的,但是之前完全没有碰到过,也没有想到,这么简单直接降低复杂度的方法 先将两个集合合并成1个集合,合并两个(s1,s2),即每个集合里n^2个数,还剩一个n个数的集合 这样还剩超时 ...

  10. 去除express.js 3.5中报connect.multipart() will be removed in connect 3.0的警告

     1 $ node app.js  2 connect.multipart() will be removed in connect 3.0  3 visit https://github.com/s ...