一開始採用递归写。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 通配符匹配(贪心)的更多相关文章

  1. [Leetcode] Wildcard matching 通配符匹配

    Implement wildcard pattern matching with support for'?'and'*'. '?' Matches any single character. '*' ...

  2. [LeetCode] Wildcard Matching 外卡匹配

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. 044 Wildcard Matching 通配符匹配

    实现一个支持 '?' 和 '*' 的通配符匹配.'?' 匹配任何单个字符.'*' 匹配任何数量的字符 (包括0个).匹配应覆盖 整个 输入字符串(而不是部分).这个函数原型为:bool isMatch ...

  4. [LeetCode] 44. Wildcard Matching 外卡匹配

    Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '? ...

  5. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  6. [LeetCode] Wildcard Matching 题解

    6. Wildcard Matching 题目 Implement wildcard pattern matching with support for '?' and '*'. '?' Matche ...

  7. [Leetcode] Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  8. [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. [leetcode]Wildcard Matching @ Python

    原题地址:https://oj.leetcode.com/problems/wildcard-matching/ 题意: Implement wildcard pattern matching wit ...

随机推荐

  1. Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time

    题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...

  2. C/C++中的位运算符

    --------开始-------- 我自己都记不住这是第几次把这几个位运算符搞混了,刚好在刚用过来把这几个位运算符记下来,俗话说的好好记性不如个烂笔头. 运算符: 与           或    ...

  3. BZOJ 1407 exgcd

    思路: 数据范围不大.. 那我们就枚举M好了.. 再两两判断一下有没有冲突 怎么判断呢? exgcd!!! p[i]*k+c[i]=p[j]*k+c[j]  (mod m) (p[j]-p[i])*k ...

  4. MyEclipse中快速复制粘贴当前行的操作

  5. Spring + Redis ( 简单使用)

    1.Redis 的 Java API Java 中 使用 Redis 工具,要先去 maven 仓库中,下载 jedis jar包 jedis 依赖 <dependency> <gr ...

  6. Tour UVA - 1347

    John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...

  7. JSP_内置对象_out

    out对象是JspWriter类的实例,是向客户端输出内容的常用对象,常用方法如下: void println() 向客户端打印字符串 void clear() 清除缓冲区的内容,如果在flush之后 ...

  8. NW.js构建PC收银端安装程序的指南

    1.首先下载nw.js的SDK: https://nwjs.org.cn/download.html 2.SDK目录下新建myapp文件夹: 3.myapp文件夹内新建package.json文件: ...

  9. Java---23种设计模式(九)------组合模式

    一.什么是组合模式 组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象. 组合模式依据树形结构来组合对象,用来表示部分以及整体层次. 这种类型的 ...

  10. 【转载】MySQL之CONCAT()的用法

    mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT()函数,供您参考 mysql CONCAT(str1,st ...