题解 | 字符串通配符

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

全部评论

相关推荐

07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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