[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",里面是否包含另一个字符 ...
随机推荐
- Unexpected namespace prefix "xmlns" found for tag Linear Layout
原文地址http://blog.csdn.net/taxuexumei/article/details/41523419 今天遇到的问题,,,保存到博客里,下回遇到找博客就行了,,,,,, 今天在制作 ...
- iOS开发---集成百度地图完善版
一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...
- springmvc中@PathVariable和@RequestParam的区别
顾名思义, @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数. 我的url; http://localhost:808 ...
- Head First Design Patterns
From Head First Design Patterns. Design Principle: Idnetify the aspects of your application that var ...
- js系列(10)js的运用(二)
本节继续介绍在html页面中js的运用. (1)数码时钟:(http://files.cnblogs.com/files/MenAngel/text05.zip) <!DOCTYPE ...
- Android之把手机的3g流量共享出来让其他人连接这个wifi
转自:http://blog.csdn.net/luoboo525/article/details/7883998 亲测可用 用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi ...
- Entity Framework 5.0基础系列
1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...
- nginx 重写 rewrite 基础及实例
nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...
- 从《BLAME!》说开去——新一代生产级卡通真实感混合的渲染方案
<BLAME!>是Polygon Pictures Inc.(以下简称PPI)创业33周年以来制作的第一部CG剧场电影,故事来自于贰瓶勉的同名漫画作品(中文译名为<探索者>或者 ...
- 直播技术资源站 http://lib.csdn.net/base/liveplay/structure
直播技术资源站 http://lib.csdn.net/base/liveplay/structure