题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
while True: try: s1 = input() s2 = input() # 初始化dp数组 dp = [[False for j in range(len(s2) + 1)] for i in range(len(s1) + 1)] dp[0][0] = True # 第0行全部为False(因为空字符无法匹配非空字符) for j in range(1, len(s2) + 1): dp[0][j] = False # 处理第0列,因为这一列可以提前预判 for i in range(1, len(s1) + 1): if s1[i - 1] == "*" or s1[i - 1] == "?": # 当模式为*或者?时,能匹配 dp[i][0] = True else: break # why? for i in range(1, len(s1) + 1): for j in range(1, len(s2) + 1): if s1[i - 1].lower() == s2[j - 1].lower(): dp[i][j] = dp[i - 1][j - 1] elif s1[i - 1] == "?" and ( (s2[j - 1].isalnum()) or (s2[j - 1].isalpha()) ): # s2[j-1]不为特殊符号 dp[i][j] = dp[i - 1][j - 1] elif s1[i - 1] == "*": dp[i][j] = dp[i - 1][j] or dp[i][j - 1] if dp[len(s1)][len(s2)]: print("true") else: print("false") except: break