44. Wildcard Matching

来源:互联网 发布:黑客页面源码 编辑:程序博客网 时间:2024/06/07 23:42
'?' 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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "*") → trueisMatch("aa", "a*") → trueisMatch("ab", "?*") → trueisMatch("aa
bool isMatch(string s, string p) {        int Slen=s.length(),Plen=p.length(),i,j,iStart=-1,jStart=-1;        for(i=0,j=0;i<Slen;++i,++j)        {            if(p[j]=='*')            {                iStart=i;                jStart=j;                --i;            }            else            {                if(p[j]!=s[i] && p[j]!='?')                {                    if(iStart>=0)                    {                        i=iStart++;                        j=jStart;                    }                    else                        return false;                }            }        }        while(p[j]=='*') ++j;        return j==Plen;    }
原创粉丝点击