c_cpp wildcard_matching,匹配'?'和'*'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp wildcard_matching,匹配'?'和'*'相关的知识,希望对你有一定的参考价值。

// here is easier to understand. 11/12/2015
// http://shmilyaw-hotmail-com.iteye.com/blog/2154716
// spend long time to understand, still hard to understand. 7/27/2014

bool isMatch(const char *s, const char *p) {
        if (*p == '\0' || *s == '\0') return *p == *s;
        if (*p == *s || *p == '?') return isMatch(++s,++p);
        if (*p == '*'){
            while(*p == '*') ++p; // ignore multiple stars
            if (*p == '\0') return true;
            // now *p is not a star. 
            while(*s != '\0' && !isMatch(s,p)){
                ++s;                
            }
            return *s != '\0';
        }
        return false;
    }


// http://www.darrensunny.me/leetcode-wildcard-matching-2/
/*
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
*/

/*
This problem is similar to a previous one LeetCode - Regular Expression Matching. 
But a recursive brute-force approach would not make it pass LeetCode OJ due to 
the exceeding of the time limit. Alternatively, we can apply dynamic programming 
techniques to avoid duplicate computation during the brute-force search. 

Specifically, we maintain a table t[i][j] , where t[i][j] is true if p[0...i] 
matches s[0...j] . Based on whether p[i] is a wildcard, we can decide t[i][j] 
accordingly:

p[i]≠′∗′ : t[i][j] is true if and only if t[i−1][j−1] is true and s[j] and 
p[i] match (i.e., p[i]=′?′ or s[j]=p[i] )

p[i]=′∗′ : t[i][j] is true if and only if any of t[i−1][j′] is true, where 0≤j′<j .
Since when calculating each cell in the table, only the elements in the preceding 
row is used, the whole table would be unnecessary but an array suffices.
*/
    
  



以上是关于c_cpp wildcard_matching,匹配'?'和'*'的主要内容,如果未能解决你的问题,请参考以下文章

遇见未来 | PostgreSQL:一匹即将发力的黑马

leofs 对象存储中一匹黑马

云图说|OLAP开源引擎的一匹黑马,MRS集群组件之ClickHouse

云图说|OLAP开源引擎的一匹黑马,MRS集群组件之ClickHouse

Vue为啥可以成为2019年的一匹黑马?

云图说丨OLAP开源引擎的一匹黑马,MRS集群组件之ClickHouse