题解 | #字符串通配符#

字符串通配符

https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036

#include <iostream>

using namespace std;

bool match(const char* p, const char* s) {
    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(p + 1, s + 1);
    } else if (*p == '*') {
        while (*p == '*') p++;
        p--;
        if (!isdigit(*s) && !isalpha(*s)) return false;
        // *匹配一个,匹配0个,匹配多个
        return match(p + 1, s + 1) || match(p + 1, s) || match(p, s + 1);
    } else if (tolower(*p) == tolower(*s)) {
        return match(p + 1, s + 1);
    }
    return false;
}

int main() {
    string s1, s2;
    while (cin >> s1 >> s2) {
        bool flag = match(s1.c_str(), s2.c_str());
        if (flag) cout << "true" << endl;
        else cout << "false" << endl;
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
02-12 10:05
小米集团 算法工程师 28.0k*15.0
泡沫灬一触即破:楼上那个看来是看人拿高薪,自己又不如意搁这泄愤呢是吧,看你过往评论很难不怀疑你的精神状态
点赞 评论 收藏
分享
穿件外套出门:这简历一眼太水了,前面有的没的直接删,写项目亮点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务