通配符匹配字符串

问题:实现支持?和*两个通配符的字符串匹配函数。

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

回忆一下类似的一个问题《简单的正则表达式匹配 Regular Expression Matching》,注意其中的区别:两个问题中*所具备的匹配能力是不同的。

思路一:递归求解。虽然已经把重复出现的*过滤,不过超时了

  1. class Solution {  
  2. public:  
  3.     bool isMatch(const char *s, const char *p) {  
  4.         if(s == NULL || p == NULL)  
  5.             return false;  
  6.         return isValid(s, p);  
  7.     }  
  8.       
  9.     bool isValid(const char *s, const char *p)  
  10.     {  
  11.         if(*p == '\0')  
  12.             return *s == '\0';  
  13.               
  14.         if(*p == '?')  
  15.             return isValid(s+1, p+1);  
  16.         else if(*p != '*')  
  17.         {  
  18.             if(*p == *s)  
  19.                 return isValid(s+1, p+1);  
  20.             else  
  21.                 return false;  
  22.         }  
  23.         else  
  24.         {  
  25.             p++;  
  26.             while(*p == '*')  
  27.                 p++;  
  28.   
  29.             while(*s != '\0')  
  30.             {  
  31.                 if(isValid(s, p))  
  32.                     return true;  
  33.                 s++;  
  34.             }  
  35.             return isValid(s, p);  
  36.         }  
  37.     }  
  38. };  
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if(s == NULL || p == NULL)
            return false;
        return isValid(s, p);
    }
    
    bool isValid(const char *s, const char *p)
    {
        if(*p == '\0')
            return *s == '\0';
            
        if(*p == '?')
            return isValid(s+1, p+1);
        else if(*p != '*')
        {
            if(*p == *s)
                return isValid(s+1, p+1);
            else
                return false;
        }
        else
        {
            p++;
            while(*p == '*')
                p++;

            while(*s != '\0')
            {
                if(isValid(s, p))
                    return true;
                s++;
            }
            return isValid(s, p);
        }
    }
};
思路二: 动态规划法

设置状态量H[pn+1][sn+1]。H[i][j]表示p的前i个字符能否匹配成功s的前j个字符。

递推关系:如果H[i-1][j-1]=1,若p[i]='?'或者p[i]==s[j],那么H[i][j]为1;若p[i]='*',那么H[i][j-1]到H[i][sn]都为1。

初始条件:H[0][0]=1。

注意:必须要提前把不可能匹配的情况排除,否则会超时。当p串中非*字符的个数大于0且少于s串的字符个数时,匹配不可能成功。

  1. class Solution {  
  2. public:  
  3.     bool isMatch(const char *s, const char *p) {  
  4.         if(s == NULL || p == NULL)  
  5.             return false;  
  6.               
  7.         //计数:记录s的字符个数、p的字符个数、*的个数   
  8.         const char *p1;  
  9.         p1 = p;  
  10.         int stars = 0;  
  11.         while(*p1 != 0)  
  12.         {  
  13.             if(*p1 == '*')  
  14.                 stars++;  
  15.             p1++;  
  16.         }  
  17.         int pn = p1 - p;  
  18.           
  19.         p1 = s;  
  20.         while(*p1 != 0)  
  21.             p1++;  
  22.         int sn = p1 - s;  
  23.           
  24.         if(pn == stars && stars > 0) //p串中只有*的情况   
  25.             return true;  
  26.         if(pn - stars > sn) //p串中非*字符的个数少于s串,不可能匹配   
  27.             return false;  
  28.           
  29.         int H[pn+1][sn+1];  
  30.         memset(H,0 ,sizeof(H));  
  31.   
  32.         H[0][0] = 1;  
  33.         for(int i=1;i<=pn;i++)  
  34.         {  
  35.             if(p[i-1] != '*')  
  36.                 break;  
  37.             H[i][0] = 1;  
  38.         }  
  39.           
  40.         for(int i=1;i<=pn;i++)  
  41.         {  
  42.             for(int j=1;j<=sn;j++)  
  43.             {  
  44.                 if(H[i-1][j-1] == 1)  
  45.                 {  
  46.                     if(p[i-1] == '?' || p[i-1] == s[j-1])  
  47.                     {  
  48.                         H[i][j] = 1;  
  49.                     }  
  50.                     else if(p[i-1] == '*')  
  51.                     {  
  52.                         for(int k=j-1;k<=sn;k++)  
  53.                             H[i][k] = 1;  
  54.                     }  
  55.                 }  
  56.             }  
  57.         }  
  58.         //当p串以*结尾时,与s的匹配有可能提前结束。   
  59.         int last;  
  60.         for(last=pn;last>=0;last--)  
  61.             if(H[last][sn] == 1)  
  62.                 break;  
  63.         last++;  
  64.         while(last<=pn && p[last-1] == '*')  
  65.             last++;  
  66.         if(last == pn+1)  
  67.             return true;  
  68.   
  69.         return H[pn][sn] == 1;  
  70.     }  
  71. };  
class Solution {
public:
    bool isMatch(const char *s, const char *p) {
        if(s == NULL || p == NULL)
            return false;
            
        //计数:记录s的字符个数、p的字符个数、*的个数
        const char *p1;
        p1 = p;
        int stars = 0;
        while(*p1 != 0)
        {
            if(*p1 == '*')
                stars++;
            p1++;
        }
        int pn = p1 - p;
        
        p1 = s;
        while(*p1 != 0)
            p1++;
        int sn = p1 - s;
        
        if(pn == stars && stars > 0) //p串中只有*的情况
            return true;
        if(pn - stars > sn) //p串中非*字符的个数少于s串,不可能匹配
            return false;
        
        int H[pn+1][sn+1];
        memset(H,0 ,sizeof(H));

        H[0][0] = 1;
        for(int i=1;i<=pn;i++)
        {
            if(p[i-1] != '*')
                break;
            H[i][0] = 1;
        }
        
        for(int i=1;i<=pn;i++)
        {
            for(int j=1;j<=sn;j++)
            {
                if(H[i-1][j-1] == 1)
                {
                    if(p[i-1] == '?' || p[i-1] == s[j-1])
                    {
                        H[i][j] = 1;
                    }
                    else if(p[i-1] == '*')
                    {
                        for(int k=j-1;k<=sn;k++)
                            H[i][k] = 1;
                    }
                }
            }
        }
        //当p串以*结尾时,与s的匹配有可能提前结束。
        int last;
        for(last=pn;last>=0;last--)
            if(H[last][sn] == 1)
                break;
        last++;
        while(last<=pn && p[last-1] == '*')
            last++;
        if(last == pn+1)
            return true;

        return H[pn][sn] == 1;
    }
};

思路三:在网上看到的优化方法。记录前一个*字符的位置,优先进行单字符匹配,当失败的时候再回来进行通配。

  1. class Solution {  
  2. public:  
  3. bool isMatch(const char *s, const char *p) {  
  4.     if(!s && !p) return true;  
  5.   
  6.     const char *star_p=NULL,*star_s=NULL;  
  7.   
  8.     while(*s)  
  9.     {  
  10.         if(*p == '?' || *p == *s)  
  11.         {  
  12.             ++p,++s;  
  13.         }else if(*p == '*')  
  14.         {  
  15.             //skip all continuous '*'   
  16.             while(*p == '*') ++p;  
  17.   
  18.             if(!*p) return true//if end with '*', its match.   
  19.   
  20.             star_p = p; //store '*' pos for string and pattern   
  21.             star_s = s;  
  22.         }else if((!*p || *p != *s)  && star_p)  
  23.         {  
  24.             s = ++star_s; //skip non-match char of string, regard it matched in '*'   
  25.             p = star_p; //pattern backtrace to later char of '*'   
  26.         }else  
  27.             return false;  
  28.     }  
  29.   
  30.     //check if later part of p are all '*'   
  31.     while(*p)  
  32.         if(*p++ != '*')  
  33.             return false;  
  34.   
  35.     return true;  
  36. }  
  37. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值