题解 | #字符串通配符#

字符串通配符

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

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

unordered_map<string, bool> m;
string s1,s2;

bool dp(int i,int j){
    if(i==s1.length()) return j==s2.length();
    if(j==s2.length()) {
        while(s1[i]=='*'){
            i++;
        }
        if(i==s1.length()) return true;
        return false;
    }
    string key=to_string(i)+','+to_string(j);
    bool f;
    if(m.count(key)==1) return m[key];
    if(toupper(s1[i])==toupper(s2[j])) f=dp(i+1,j+1);
        else if(s1[i]=='?') {
            if (isdigit(s2[j]) || isalpha(s2[j])) {
                f=dp(i+1,j+1);
            } else {
                f = false;
            }
        }
            else if(s1[i]=='*') f=dp(i,j+1)||dp(i+1,j);
                else f=false;
    m[key]=f;
    return f;
}


int main() {
    cin>>s1>>s2;
    if(dp(0,0)) cout<<"true";
        else cout<<"false";
    return 0;
}
// 64 位输出请用 printf("%lld")

niu

全部评论

相关推荐

12-19 15:04
门头沟学院 Java
小肥罗:hr爱上你了,你负责吗哈哈
点赞 评论 收藏
分享
12-24 20:49
武汉大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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