题解 | #字符串通配符#
字符串通配符
https://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
def regexp(rule, s): rule = rule.upper() s = s.upper() start = rule[0] ss = start for i in range(1, len(rule)): if (start == "*" and rule[i] == "*"): continue else: ss += rule[i] start = rule[i] rule=ss rule1 = rule s1 = s for i in range(len(rule)): if (len(s) - 1) >= i: if rule[i] == s[i] or (rule[i] == "?" and (ord("Z") >= ord(s[i]) >= ord("A") or s[i] == "?" or "9">=s[i]>="0")): rule1 = rule[(i + 1):] s1 = s[(i + 1):] elif(rule[i]=="*"): break else: return False else: for i in range(len(rule1)): if ord("Z") >= ord(rule1[i]) >= ord("A") or rule1[i] == "?" or "9">=rule1[i]>="0": return False return True rule=rule1 s=s1 for i in range(-1, -1 * (len(rule) + 1), -1): if len(s) * -1 <= i: if rule[i] == s[i] or (rule[i] == "?" and (ord("Z") >= ord(s[i]) >= ord("A") or s[i] == "?" or "9">=s[i]>="0")): rule1 = rule[:i] s1 = s[:i] elif (rule[i] == "*"): break else: return False else: for i in range(len(rule1)): if ord("Z") >= ord(rule1[i]) >= ord("A") or rule1[i] == "?": return False return True if len(rule1)==0: if len(s1)==0: return True else: return False if rule1.strip("*") !="": rule1= rule1.strip("*") else: for x in s1: if("0">=x>="9" or "Z">=x>="A"): continue else: return False return True for i in range(len(s1)): for j in range(i, len(s1)): flag = regexp(rule1, s1[i:j+1]) if (flag): return True return False rule = input() s = input() print("true") if regexp(rule, s) else print("false")