题解 | #字符串通配符#

字符串通配符

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

#include <bits/stdc++.h>
using namespace std;


string c, d;
bool strmatch(int i, int j){
    if (i >= c.size() and j >= d.size()) return true;
    if (i >= c.size() or j >= d.size()) return false;
    if (c[i]=='?'){
        if (isalpha(d[j]) or isdigit(d[j])) return strmatch(i+1, j+1);
        return false;
    }
    else if (c[i]=='*'){
        if (isalpha(d[j]) or isdigit(d[j])) return strmatch(i+1, j) || strmatch(i, j+1) || strmatch(i+1, j+1);
        else return strmatch(i+1, j);
    }
    else {
        if (c[i] != d[j]) return false;
        return strmatch(i+1, j+1);
    }
}


int main(){
    string a, b;
    cin >> a >> b;
    bool hasstar = false;
    for(int i = 0; i < a.size(); i++){
        if (!hasstar or a[i] != '*') c.push_back(tolower(a[i]));
        if (a[i] == '*') hasstar=true;
        if (a[i] != '*') hasstar=false;
    }
    for(int i = 0; i < b.size(); i++) d.push_back(tolower(b[i]));
    // cout << c << " " << d << endl;
    if (strmatch(0, 0)) cout << "true";
    else cout << "false";
    return 0;
}

全部评论

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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