[LeetCode]Wildcard Matching 通配符匹配(贪心)
一開始採用递归写。TLE。
class Solution {
public:
bool flag;
int n,m;
void dfs(int id0,const char *s,int id1,const char *p){
if(flag)return;
if(id0>=n){
if(id1>=m)flag=1;
else{
int j=0;
while(j<m&&p[j]=='*')j++;
if(j>=m)flag=1;
}
return;
}
else if(id1>=m)return;
if(p[id1]=='? '||p[id1]==s[id0]){
dfs(id0+1,s,id1+1,p);
}
else if(p[id1]=='*'){
for(int j=id0;j<n;++j) dfs(j,s,id1+1,p);
}
}
bool isMatch(const char *s, const char *p) {
for(n=0;s[n];++n);
for(m=0;p[m];++m);
flag=0;
dfs(0,s,0,p);
return flag;
}
};
粗剪枝了,还是超时。
看了下Discuss,发现能够贪心:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
const char*lastp=NULL,*lasts=NULL;
while(*s){
if(*s==*p||*p=='?'){
p++;
s++;
}
else if(*p=='*'){
//不匹配,仅仅记录
lastp=p++;
lasts=s;
}
else if(lastp!=NULL){
p=lastp+1;
s=++lasts;//逐渐递增匹配1个字符、2个字符...
}
else return false;
}
while(*p&&*p=='*')p++;
return *p=='\0';
}
};
採用DP:
用dp[i,j]表示s[0,..,i]与p[0,..,j]是否匹配
dp[i,j]=1 if (p[j]=='*'&&(dp[i-1,j]||dp[i][j-1]||dp[i-1][j-1]))
dp[i,j]=1 if ((p[j]=='?'||s[i]==p[j]])&&dp[i-1][j-1])
时间复杂度O(nm),能够採用滚动数组,否则会超内存。空间复杂度O(m)
可是,会超时
bool isMatch(const char *s, const char *p) {
int n=strlen(s),m=strlen(p),i,j;
bool **dp=new bool*[2];
for(i=0;i<2;++i){
dp[i]=new bool[m+1];
memset(dp[i],0,sizeof(bool)*(m+1));
}
dp[0][0]=1;
bool flag=0;
for(i=1;i<=n;++i){
for(j=1;j<=m;++j){
if(dp[flag][j-1]&&(p[j-1]=='?'||s[i-1]==p[j-1]))
dp[1-flag][j]=1;
else if(p[j-1]=='*'&&(dp[flag][j-1]||dp[flag][j]||dp[1-flag][j-1]))
dp[1-flag][j]=1;
}
flag=1-flag;
}
bool ans=dp[1][m];
for(i=0;i<2;++i)delete[] dp[i];
delete[]dp;
return ans;
}
[LeetCode]Wildcard Matching 通配符匹配(贪心)的更多相关文章
- [Leetcode] Wildcard matching 通配符匹配
Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- 044 Wildcard Matching 通配符匹配
实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...
- [LeetCode] 44. Wildcard Matching 外卡匹配
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...
- LeetCode: Wildcard Matching 解题报告
Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...
- [LeetCode] Wildcard Matching 题解
6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...
- [Leetcode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- [leetcode]Wildcard Matching @ Python
原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...
随机推荐
- form内部的button_to不submit
创建: 2017/09/12 更新: 2018/03/17 修正因为博客迁移造成的格式问题 官方文档 http://railsdoc.com/references/button_to 参考文档 htt ...
- Python 40 数据库-约束
约束 1.什么是约束 ? 除了数据类型以外额外添加的约束 2.为什么要使用约束 ? 为了保证数据的合法性 完整性 分类: 1.not null 非空约束,数据不能为空----------------- ...
- Spark2.2,IDEA,Maven开发环境搭建附测试
前言: 停滞了一段时间,现在要沉下心来学习点东西,出点货了. 本文没有JavaJDK ScalaSDK和 IDEA的安装过程,网络上会有很多文章介绍这个内容,因此这里就不再赘述. 一.在IDEA上安装 ...
- 努比亚 N1 (Nubia NX541J) 解锁BootLoader 并刷入recovery
本篇教程教你如何傻瓜式解锁BootLoader并刷入recovery 仅限于努比亚 N1 (Nubia NX541J)使用. 首先需要在设置-关于手机 找到版本号 然后手机快速连续点击来启用开发者模式 ...
- Android贝塞尔曲线应用-跳动的水滴
主要通过6个控制点实现. val startPoint = PointF() val endPoint = PointF() val control1 = PointF() val control2 ...
- MatLab之HDL coder
1 Workflow The workflow for applying HDL code generation to the hardware design process requires the ...
- ROS:使用ubuntuKylin17.04安装ROS赤xi龟
使用ubuntuKylin17.04安装 参考了此篇文章:SLAM: Ubuntu16.04安装ROS-kinetic 重复官方链接的步骤也没有成功. 此后发现4.10的内核,不能使用Kinetic. ...
- Maya API编程快速入门
一.Maya API编程简介 Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can ch ...
- linux环境jdk安装及配置
linux环境jdk安装及配置 linux环境jdk安装及配置 1.下载jkd( http://www.oracle.com/technetwork/java/javase/downloads/ind ...
- 【sqli-labs】 less9 GET - Blind - Time based. - Single Quotes (基于时间的GET单引号盲注)
加and http://localhost/sqli/Less-9/?id=1' and '1'='1%23 http://localhost/sqli/Less-9/?id=1' and '1'=' ...