[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 ...
随机推荐
- Mysql数据库系列
详情点击 MySQL基础 Mysql表操作 Mysql插入 更新 删除 查询操作 Mysql创建用户和授权 基本的Mysql语句 Mysql库的操作 Mysql表的操作 Mysql数据类型(一) My ...
- Java中数组要点总结
1.数组是基本数据类型和字符串类型的容器(引用数据类型),而集合是类数据类型的容器: 2.数组定义的格式: (1)一般格式: 元素类型[] 数组名 = new 元素类型[元素个数或者数组长度]: 其中 ...
- 5.27 sorm图
- 关于如何将_variant_t型转化为int型和string类型
1)将_variant_t型转化为int型 关于将_variant_t型转化为int型,网上有好多好多参考,但好多都很复杂并且还不对,其实整个转化过程就只一行代码可以搞定: _variant_t a; ...
- iOS多线程——GCD篇
什么是GCD GCD是苹果对多线程编程做的一套新的抽象基于C语言层的API,结合Block简化了多线程的操作,使得我们对线程操作能够更加的安全高效. 在GCD出现之前Cocoa框架提供了NSObjec ...
- VMWare 安装Centos 6.9
1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...
- List 练习
(List)已知有一个Worker 类如下: public class Worker { private int age; private String name; private double sa ...
- Python之first script
1 A first script 1) script1.py - imports a Python module (libraries of additional tools) to fetch th ...
- python3:语法变动 及新特性
python3.0 对python2.x 升级后重大语法变动,幸好留下2.7.6及后续2版本,保持一些语法兼容. 原始地址:http://hi.baidu.com/jxq61/item/3a24883 ...
- What is the difference between PKCS#5 padding and PKCS#7 padding
The difference between the PKCS#5 and PKCS#7 padding mechanisms is the block size; PKCS#5 padding is ...