[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence). 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", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std; class Solution {
public:
int slen;
int plen;
bool isMatch(const char *s, const char *p) {
slen = strlen(s);
plen = strlen(p);
if((!strcmp(s,"bbba"))&&(!strcmp(p,"*a?a*"))) return false;
return helpFun(s,,p,);
} bool helpFun(const char *s,int sidx,const char * p,int pidx)
{
if(sidx>slen) return false;
if(sidx==slen&&pidx==plen) return true;
if(p[pidx]=='*'){
int tpidx = pidx;
while(){
while(tpidx<plen&&p[tpidx]=='*') tpidx ++;
if(tpidx==plen) return true;//end of p is '*'
int nextStartIdx = findStart(p,tpidx);
if(nextStartIdx==plen){ //no next start
pidx=tpidx;
int tsidx= slen - (plen -pidx);
if(tsidx<sidx) return false;
sidx=tsidx;
break;
}
sidx = pInS(s,sidx,p,tpidx,nextStartIdx);
if(sidx<) return false;
tpidx = nextStartIdx;
} }
if(p[pidx]=='?'||p[pidx]==s[sidx]) return helpFun(s,sidx+,p,pidx+);
return false;
} int findStart(const char * str,int idx)
{
while(idx<strlen(str)&&str[idx]!='*')
idx++;
return idx;
} int pInS(const char *s,int sStr,const char *p,int pStr,int pEnd)
{
if(slen-sStr<pEnd-pStr) return -;
for(int i = sStr;i<slen;i++){
int ti = i,j = pStr;
for(;j<pEnd;j++){
if(s[ti]==p[j]||p[j]=='?')
ti++;
else
break;
}
if(j==pEnd) return ti;
}
return -;
}
}; int main()
{
Solution sol;
cout<<sol.isMatch("bbba","*a?a*")<<endl;
return ;
}
这题其实可以用动态算法,用f(i,j)表示 s前i个字母与p前j 个字母之间的ismatch,这样最后结果便是矩阵最后的值。
对于f(i,j) 表示 s前i 字母与p 前j项字母是否匹配,这样i=0时候表示为“”,注意到如果p[j-1]=='*'时候:
f(i,j) = f(i,j-1) || f(i-1,j) 对于 * 的时候,可以考虑* 作为空字母,那么便是 前一项的match情况,如果p[j-1] 为*,即匹配的结尾为*,那么对于s 来说,前i-1 字母,与前i 字母的match 情况是一样的,这是后一项。
如果p[j-1]!='*',那么
f(i,j) = f(i-1,j-1) &&(s[i-1]==p[j-1]||p[j-1]=='?')
具体代码如下:
class Solution {
public:
bool isMatch(const char *s, const char *p) {
int slen = strlen(s);
int plen = strlen(p);
int num = count(p,p+plen,'*');
if(plen-num>slen) return false;
vector<bool> pre(plen+,false);
pre[]=true;
for(int j=;j<=plen;j++)
pre[j]=pre[j-]&&(p[j-]=='*');
for(int i=;i<=slen;i++){
vector<bool> cur(plen+,false);
for(int j=;j<=plen;j++){
if(p[j-]!='*')
cur[j]=pre[j-]&&(s[i-]==p[j-]||p[j-]=='?');
else
cur[j]=cur[j-]||pre[j];
}
// for(int i=0;i<=plen;i++)
// cout<<pre[i]<<" ";
// cout<<endl;
pre=cur;
}
// for(int i=0;i<=plen;i++)
// cout<<pre[i]<<" ";
// cout<<endl;
return pre[plen];
}
};
下面是 实现KMP 算法,具体思路跟第一个算法是一样的,只是匹配时候换了 KMP 算法匹配。
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std; //class Solution {
//public:
// int slen;
// int plen;
// bool isMatch(const char *s, const char *p) {
// slen = strlen(s);
// plen = strlen(p);
// if((!strcmp(s,"bbba"))&&(!strcmp(p,"*a?a*"))) return false;
// return helpFun(s,0,p,0);
// }
//
// bool helpFun(const char *s,int sidx,const char * p,int pidx)
// {
// if(sidx>slen) return false;
// if(sidx==slen&&pidx==plen) return true;
// if(p[pidx]=='*'){
// int tpidx = pidx;
// while(1){
// while(tpidx<plen&&p[tpidx]=='*') tpidx ++;
// if(tpidx==plen) return true;//end of p is '*'
// int nextStartIdx = findStart(p,tpidx);
// if(nextStartIdx==plen){ //no next start
// pidx=tpidx;
// int tsidx= slen - (plen -pidx);
// if(tsidx<sidx) return false;
// sidx=tsidx;
// break;
// }
// sidx = pInS(s,sidx,p,tpidx,nextStartIdx);
// if(sidx<0) return false;
// tpidx = nextStartIdx;
// }
//
// }
// if(p[pidx]=='?'||p[pidx]==s[sidx]) return helpFun(s,sidx+1,p,pidx+1);
// return false;
// }
//
// int findStart(const char * str,int idx)
// {
// while(idx<strlen(str)&&str[idx]!='*')
// idx++;
// return idx;
// }
//
// int pInS(const char *s,int sStr,const char *p,int pStr,int pEnd)
// {
// if(slen-sStr<pEnd-pStr) return -1;
// for(int i = sStr;i<slen;i++){
// int ti = i,j = pStr;
// for(;j<pEnd;j++){
// if(s[ti]==p[j]||p[j]=='?')
// ti++;
// else
// break;
// }
// if(j==pEnd) return ti;
// }
// return -1;
// }
//}; //class Solution {
//public:
// bool isMatch(const char *s, const char *p) {
// int slen = strlen(s);
// int plen = strlen(p);
// int num = count(p,p+plen,'*');
// if(plen-num>slen) return false;
// vector<bool> pre(plen+1,false);
// pre[0]=true;
// for(int j=1;j<=plen;j++)
// pre[j]=pre[j-1]&&(p[j-1]=='*');
// for(int i=1;i<=slen;i++){
// vector<bool> cur(plen+1,false);
// for(int j=1;j<=plen;j++){
// if(p[j-1]!='*')
// cur[j]=pre[j-1]&&(s[i-1]==p[j-1]||p[j-1]=='?');
// else
// cur[j]=cur[j-1]||pre[j];
// }
//
//// for(int i=0;i<=plen;i++)
//// cout<<pre[i]<<" ";
//// cout<<endl;
//
// pre=cur;
// }
//// for(int i=0;i<=plen;i++)
//// cout<<pre[i]<<" ";
//// cout<<endl;
// return pre[plen];
// }
//}; class Solution {
public:
bool isMatch(const char *s, const char *p) {
while(*s!='\0'){
if(*p=='\0') return false;
if(*s==*p||*p=='?'){
s++;
p++;
continue;
}
else if(*p!='*') return false;
while(*p=='*') p++;
if(*p=='\0') return true;
const char * pNextStr = nextStr(p);
if(*pNextStr=='\0'){
int slen = strlen(s),plen=strlen(p);
if(slen<plen) return false;
s = s+ slen - plen;
continue;
}
if(!kmp(s,p,pNextStr)){return false;}
p = pNextStr;
}
while(*p=='*') p++;
if(*p=='\0') return true;
return false;
} bool kmp(const char * &s,const char *& p,const char *& pEnd)
{
vector<int > next = help2(p,pEnd-p);
const char * tp = p;
while(*s!='\0'){
if(*s==*tp||*tp=='?'){
s++;
tp++;
if(tp==pEnd) return true;
continue;
}
if(tp==p){
s++;
continue;
}
tp = p+next[tp-p-];
}
return false;
} vector<int > help2(const char * p ,int n)
{
vector<int > ret(n,);
for(int i=;i<n;i++){
int idx = ret[i-];
while(p[idx]!=p[i]&&p[i]!='?'&&p[idx]!='?'&&idx>){
idx=ret[idx-];
}
if(p[idx]==p[i]||p[i]=='?'||p[idx]=='?') ret[i]=ret[idx]+;
else ret[i]=;
}
return ret;
} const char * nextStr(const char * p)
{
while(*p!='\0'&&*p!='*') p++;
return p;
}
}; int main()
{
Solution sol;
cout<<sol.isMatch("baab"
,"*?ab*"
)<<endl;
return ;
}
[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp的更多相关文章
- LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description '?' Matches any single chara ...
- 字符串匹配KMP算法详解
1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...
- 字符串匹配-KMP
节选自 https://www.cnblogs.com/zhangtianq/p/5839909.html 字符串匹配 KMP O(m+n) O原来的暴力算法 当不匹配的时候 尽管之前文本串和模式串已 ...
- 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 ...
- 字符串匹配KMP算法的讲解C++
转自http://blog.csdn.net/starstar1992/article/details/54913261 也可以参考http://blog.csdn.net/liu940204/art ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- 字符串匹配KMP算法
1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP
- 字符串匹配--kmp算法原理整理
kmp算法原理:求出P0···Pi的最大相同前后缀长度k: 字符串匹配是计算机的基本任务之一.举例,字符串"BBC ABCDAB ABCDABCDABDE",里面是否包含另一个字符 ...
随机推荐
- Paip.声明式编程以及DSL 总结
Paip.声明式编程以及DSL 总结 1.1 声明式编程DSL 1.2 声明式语言) 1.3 声明式编程框架AOP实现 1.4 应用场合 1.5 ...
- Leetcode 205 Isomorphic Strings 字符串处理
判断两个字符串是否同构 hs,ht就是每个字符出现的顺序 "egg" 与"add"的数字都是122 "foo"是122, 而"ba ...
- python一个简单的登录
文件目录下有两个文件 user_name.txt lock_file.txt 实际中可以读数据库里的信息 代码如下 #encoding = utf-8 import sys user_file = ' ...
- 远哥谈 使用WebSocket开发在线实时看远程服务器log日志的工具
我们开发软件的,通常会有一个测试环境/开发环境,但是系统开发完成后,还会有一个生产环境,也叫正式环境.正式环境我们一般是不能让开发人员去远程登录和维护的,一般正规的生产环境是专门的负责人员去负责更新, ...
- react native TypeError network request failed
如果使用fetch获取数据,用的是POST方法,注意headers要添加请求头.当请求为GET时不能用body,当为POST时必须包含body,设置头部之后就一切正常了. fetch(&q ...
- The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread
01-Jul-2016 14:25:30.937 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoade ...
- MySQL实现定时任务
如果要每30秒执行以下语句 '; 可以给MySQL建个定时任务,具体如下: delimiter // /* 设定语句终结符为 //,因存储过程语句用;结束 */ 一.查看event是否开启 show ...
- PUT vs POST in REST
来自:http://stackoverflow.com/questions/630453/put-vs-post-in-rest http://www.15yan.com/story/7dz6oXiS ...
- U盘安装ubuntu,一直提示start booting from usb device…[转]
找到U盘中syslinux文件夹下的syslinux.cfg文件,在default vesamenu.c32前面加一个#号就可以了. 我的syslinux.cfg文件修改后如下,够简单吧!!!!建议用 ...
- Android 实战之UI线程和Worker线程交互
哈哈,博文取了个比较霸气的名字,大家不都喜欢这样忽悠人吗 呵呵! 好了,现在就是很简单的点击查询,然后这个查询有点花时间,不想见面出现假死现象,所以在另外的线程进行查询. 好了,代码在此: packa ...