wildcard-matching

来源:互联网 发布:excel大量数据统计 编辑:程序博客网 时间:2024/06/03 07:27

题目:

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

程序:

class Solution{public:    bool isMatch(const char *s, const char *p)   {          int ls = strlen(s), lp = strlen(p);          vector<vector<bool> > vvb(ls + 1, vector<bool>(lp + 1, false));          vvb[0][0] = true;         for(int j = 1; j <= lp; ++ j)         {             vvb[0][j] = vvb[0][j - 1] && '*' == p[j - 1];             for(int i = 1; i <= ls; ++ i)             {                 if('?' == p[j - 1] || s[i - 1] == p[j - 1])                     vvb[i][j] = vvb[i - 1][j - 1];                 else if('*' == p[j - 1])                     vvb[i][j] = vvb[i][j - 1] || vvb[i - 1][j];             }         }         return vvb[ls][lp];     }};
原创粉丝点击