题解 | 字符串通配符

#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")

全部评论

相关推荐

01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
SHC2:春招先狠狠投递,然后你看看能不能申请香港新加坡的一年制master,花不了多少钱,或者现在赶紧去刷一段实习。HR专业考研没必要
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务