一開始採用递归写。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. thinkphp data方法

    data方法也是模型类的连贯操作方法之一,用于设置当前要操作的数据对象的值,可能大家不太习惯用这个方法,今天来讲解下如何用好data方法. 用法 写操作 通常情况下我们都是通过create方法或者赋值 ...

  2. json用法

    什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: 1 2 ...

  3. 模拟Queue(wait/notify)

    BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...

  4. 基于Myeclipse+Axis2的WebService开发实录

    最近开始学习了下在Myeclipse开发工具下基于WebSerivce的开发,下面将相关相关关键信息予以记录 Myeclipse的安装,本文以Myeclipse2014-blue为开发环境,相关配置执 ...

  5. for 循环练习题(2)

    一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var x=0.0001; for(var a=1;true;a++){ x=x*2; if (x>8848) { ...

  6. JWPL工具处理维基百科wikipedia数据用于NLP

    JWPL处理维基百科数据用于NLP 处理zhwiki JWPL是一个Wikipedia处理工具,主要功能是将Wikipedia dump的文件经过处理.优化导入mysql数据库,用于NLP过程.以下以 ...

  7. openMSP430之io_test

    openMSP430: IO functionality test with interupt #include "omsp_system.h" volatile char shi ...

  8. I2C controller core之Bit controller(02)

    4 generate clock and control signals 1 -- architecture signal iscl_oen, isda_oen : std_logic; -- int ...

  9. 论 fmap、fmap fmap、与 fmap fmap fmap

    https://blog.csdn.net/sinat_25226993/article/details/44415803

  10. java操作Excel的poi 格式设置

    格式设置 package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi. ...