def merge(a, b): res = [] if len(a) >= len(b): for i in range(len(b)): if a[i] != b[i]: res.append("?") else: res.append(a[i]) else: return merge(b, a) res = res + list(a[len(b):]) return "".join(res) def mod(input, progress): if not input: return "" current, rest = input[0], input[1:] cmd = current.split(" ")[0] if cmd == "+": arg1, arg2 = int(current.split(" ")[1]) - 1, int(current.split(" ")[2]) - 1 res1, res2 = [], [] if progress[arg1] == 0: progress1 = progress.copy() progress1[arg1] = 1 res1 = mod(rest, progress1) if progress[arg2] == 0: progress2 = progress.copy() progress2[arg2] = 1 res2 = mod(rest, progress2) return merge(res1, res2) if cmd == "-": arg1 = int(current.split(" ")[1]) - 1 progress1 = progress progress1[arg1] = 0 return mod(rest, progress1) if cmd == "?": arg1 = int(current.split(" ")[1]) - 1 return str(progress[arg1]) + mod(rest, progress) return "" line = input() n = int(line.split(" ")[0]) q = int(line.split(" ")[1]) cmds = [] for i in range(q): cmds.append(input()) print(mod(cmds, [0] * n))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | def merge(a, b): res = [] if len(a) >= len(b): for i in range(len(b)): if a[i] != b[i]: res.append("?") else: res.append(a[i]) else: return merge(b, a) res = res + list(a[len(b):]) return "".join(res) def mod(input, progress): if not input: return "" current, rest = input[0], input[1:] cmd = current.split(" ")[0] if cmd == "+": arg1, arg2 = int(current.split(" ")[1]) - 1, int(current.split(" ")[2]) - 1 res1, res2 = [], [] if progress[arg1] == 0: progress1 = progress.copy() progress1[arg1] = 1 res1 = mod(rest, progress1) if progress[arg2] == 0: progress2 = progress.copy() progress2[arg2] = 1 res2 = mod(rest, progress2) return merge(res1, res2) if cmd == "-": arg1 = int(current.split(" ")[1]) - 1 progress1 = progress progress1[arg1] = 0 return mod(rest, progress1) if cmd == "?": arg1 = int(current.split(" ")[1]) - 1 return str(progress[arg1]) + mod(rest, progress) return "" line = input() n = int(line.split(" ")[0]) q = int(line.split(" ")[1]) cmds = [] for i in range(q): cmds.append(input()) print(mod(cmds, [0] * n)) |