码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

时间:2015-04-07 07:09:42      阅读:441      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Hide Tags
 Dynamic Programming Backtracking Greedy String
 
 
    这题好难,开始直接是递归的,但是简单的递归会超时,后面改进是遇到‘*’特殊处理,如果有不连续的多个*号,便看下s 剩余中时候有两个 * 之间的字符串,这个可以用kmp 算法,明天写一个,现在实现是直接搜索,不连续的多个* 号之间处理后,后面便方便很多了。可是实验例子与我代码中有点问题,本地运行返回false ,oj 返回确实true。所以直接跳过该例子了。
#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,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;
    }
};

int main()
{
    Solution sol;
    cout<<sol.isMatch("bbba","*a?a*")<<endl;
    return 0;
}

 

  这题其实可以用贪心算法,用f(i,j)表示 s前i个字母与p前j 个字母之间的ismatch,这样最后结果便是矩阵最后的值。明天写下。

 

[LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp

标签:

原文地址:http://www.cnblogs.com/Azhu/p/4397341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!