原题传送门
def solve1(s, p): p = p.lower() s = s.lower() p = p.replace('.', '\.').replace('?', '[0-9a-z]').replace('*', '#') p = re.sub('#+', '[0-9a-z]*', p) if bool(re.fullmatch(p, s)): print('true') else: print('false')
import re import sys if sys.platform != "linux": sys.stdin = open("input/HJ71.txt") def solve1(s, p): p = p.lower() s = s.lower() p = p.replace('.', '\.').replace('?', '[0-9a-z]').replace('*', '#') p = re.sub('#+', '[0-9a-z]*', p) if bool(re.fullmatch(p, s)): print('true') else: print('false') def solve2(s, p): m, n = len(p), len(s) ''' 初始化边界 1. dp[0][0] = True, 空模式空字符,匹配成功 2. dp[0][j] = False, 空模式无法匹配非空字符串 3. dp[i][0] 不确定,只有模式为*时,才能匹配空字符串,即模式p前i个字符均为* ''' dp = [[False] * (n + 1) for _ in range(m + 1)] dp[0][0] = True for i in range(1, m + 1): if p[i - 1] != '*': break dp[i][0] = True for i in range(1, m + 1): for j in range(1, n + 1): if p[i - 1] == '*': 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] if dp[m][n]: print("true") else: print("false") while True: try: p = input().strip() s = input().strip() solve2(s, p) except: break