题解 | 字符串通配符

#include <cctype>
#include <iostream>
using namespace std;

bool match(const char* s, const char* p){
    if((*p=='\0')&&(*s=='\0')){
        return true;
    }

    if((*p=='\0')||(*s=='\0')){
        return false;
    }
    if(*p == '?'){
        if(!isdigit(*s)&&!isalpha(*s))
            return false;
        return match(s+1,p+1);
    }else if(*p == '*'){
        while(*p == '*'){
            p++;
        }
        p--;
        //遇到*号,匹配0个(str+1,str1不用动),匹配1个(str和str1都往前移动1位),匹配多个(str不用动,str+1)
        return match(s,p+1)||match(s+1,p+1)||match(s+1, p);
    }else if(tolower(*p) == tolower(*s)){
        return match(s+1,p+1);
    }
    return false;
}

int main() {
    string p,s;
    getline(cin,p);
    getline(cin,s);
    bool res = match(s.c_str(),p.c_str());
    if(res)
        cout<<"true"<<endl;
    else
        cout<<"false"<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务