题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
def find(p, s): ''' 字符串通配符匹配判断 :param p: 通配符 :param s: 字符串 :return: 匹配成功输出1,失败0 ''' lp, ls = len(p), len(s) ''' 初始化边界 dp[0][0]=1, 空对空,匹配成功 dp[0][j]=0, 空对非空,匹配失败 dp[i][0]无法确定, 需要下边遍历确认 ''' # 构建动态规划列表 (ls+1)为列 dp = [[0] * (ls + 1) for _ in range(lp + 1)] dp[0][0] = 1 # 初始化:空对空 # 确定d[i][0]首列,一直是*就一直填1,碰到例外就终止 for i in range(1, lp + 1): # 第一列第二行开始遍历 if p[i - 1] == '*': dp[i][0] = 1 else: break # 确定其他格的值,遍历各行各列 for i in range(1, lp + 1): for j in range(1, ls + 1): if p[i - 1] == '*' and (s[j - 1].isalnum() or dp[i-1][j]): dp[i][j] = dp[i - 1][j] or dp[i][j - 1] elif p[i - 1] == '?' and s[j - 1].isalnum(): dp[i][j] = dp[i - 1][j - 1] elif s[j - 1].lower() == p[i - 1].lower(): dp[i][j] = dp[i - 1][j - 1] return dp[i][j] if __name__ == '__main__': while 1: try: p, s = input(), input() if find(p, s): print('true') else: print('false') except: break