[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",里面是否包含另一个字符 ...
随机推荐
- Effective STL中文版 译序
<Effective STL中文版>译序 就像本书的前两本姊妹作(Effective C++.More Effective C++)一样,本书的侧重点仍然在于提升读者的经验,只不过这次将焦 ...
- node.js cluster多进程、负载均衡和平滑重启
1 cluster多进程 cluster经过好几代的发展,现在已经比较好使了.利用cluster,可以自动完成子进程worker分配request的事情,就不再需要自己写代码在master进程中rob ...
- js系列(8)简介
JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HT ...
- Thinkphp下嵌套UEditor富文本WEB编辑器
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 本文实际操作于ThinkPHP框架下,现 ...
- 用C/C++实现对STORM的执行信息查看和控制
近期公司有个需求.须要在后端应用server上实时获取STORM集群的执行信息和topology相关的提交和控制,经过几天对STORM UI和CMD源代码的分析,得出能够通过其thrift接口调用实现 ...
- fidder 抓 https包配置方法(ios & android & pc浏览器)
1. fidder抓https包的基本配置,可参见以下博文 http://blog.csdn.net/idlear/article/details/50999490 2. 遇到问题:抓包看只有Tunn ...
- [转]揭秘webdriver实现原理
转自:http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html 通过研究selenium-webdriver的源码,笔者发现其实w ...
- 用node-webkit 开发 PC 客户端
7月 3 2013 导言 node-webkit 是一个很神奇的桌面客户端项目,正如这个项目的名字,这个项目是由node 和 webkit 构成,简单来说,就是你可以用HTML 5和 node 进 ...
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- DropDownList 获取不了选择的值 这种错误
有时候做项目的时候 发现DropDownList 获取不了选择的值 这个原因很可能是 你初始化DropDownList的时候 没有进行 ispostback的判断 导致提交的时候 又初始化了一次... ...