题解 | #字符串通配符# 把c的写法抄过来改造了一下

字符串通配符

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


def compare(p, s):

    lp = len(p)
    ls = len(s)
    i = 0
    j = 0
    while i < lp and j < ls:
        if p[i] == s[j] or p[i] == "?" and s[j].isalnum():
            i += 1
            j += 1

        elif p[i] == "*":
            # 合并**
            while p[i] == "*":
                if i + 1 == lp:
                    return True
                else:
                    i += 1

            for k in range(j, ls):
                if compare(p[i:], s[k:]):
                    return True
            return False

            # 三种情况 匹配0个字符 匹配下一个字符,匹配多个(std不动,还是*在匹配,str下移)
            # return compare(p[i:],s[j:]) or compare(p[i:],s[j+1:]) or compare(p[i:],s[j+1:])

        else:
            return False

    if i == lp and j == ls:
        return True
    else:
        return False


p = input().lower()
s = input().lower()


out = "true" if compare(p, s) else "false"
print(out)

全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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