10. Regular Expression Matching *HARD*
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*的更多相关文章
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...
- leetcode 10. Regular Expression Matching 、44. Wildcard Matching
10. Regular Expression Matching https://www.cnblogs.com/grandyang/p/4461713.html class Solution { pu ...
- Leetcode 10. Regular Expression Matching(递归,dp)
10. Regular Expression Matching Hard Given an input string (s) and a pattern (p), implement regular ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- leetcode problem 10 Regular Expression Matching(动态规划)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- [LeetCode] 10. Regular Expression Matching 正则表达式匹配
Given an input string (s) and a pattern (p), implement regular expression matching with support for ...
- [LeetCode] 10. Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. DP: public class Solution { publ ...
- 【leetcode】10.Regular Expression Matching
题目描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
随机推荐
- P2503 [HAOI2006]均分数据
P2503 [HAOI2006]均分数据 模拟退火+dp (不得不说,我今天欧气爆棚) 随机出1个数列,然后跑一遍dp统计 #include<iostream> #include<c ...
- tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南(含nginx/https支持)
之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...
- CF#235E. Number Challenge
传送门 可以理解为上一道题的扩展板.. 然后我们就可以YY出这样一个式子 ${\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^cd(ijk)=\sum_{i=1}^a\sum_{ ...
- 20145329 《网络对抗技术》MSF基础应用
实践目标 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.具体需要完成(1)一个主动攻击,如ms08_067;(2)一个针对浏览器的攻击,如ms11_050:(3)一个针对客户端 ...
- 调试工具--console用法收藏
1.使用console进行性能测试和计算代码运行时间:http://www.cnblogs.com/0603ljx/p/4387628.html 2.console命令详解:http://www.cn ...
- luogu P3387 【模板】缩点
题目 好久没法博客了 这次就水个板子题目吧 tarjan缩点之后重新建图 而且边权应该都是正的(要不我怎么能这么轻松水过去) 在新图上记忆化一下就好了 f[i] 表示 开头选i这个点 的 路径最大值 ...
- springmvc-自定义消息转换器
最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧! 如有不对地方望指出! 一.自定义类实现AbstractHttpMessageConverter package com.dz ...
- 【安装】Microsoft SQL Server的安装
数据库版本:2012 系统环境:windows 7 一.安装 依次选择“安装->全新 SQL Server 独立安装或向现有安装添加功能”;点“确定” 选择版本,推荐标准版,这里是直接输入序列号 ...
- label表单的关联性
<input type="checkbox" id="cr" /> <label for="cr">点击关联复选框& ...
- Ubuntu16.04 无法连接WiFi
在安装完 ns-3.25 之后,着手开始准备 Eclipse 的安装,打开了 Firefox游览器 准备上网的时候,发现网络没有正常连接. 刚刚开始怀疑的是,并没有连接上网络. 于是打开了终端,pin ...