题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import sys commands = { "reset": "reset what", "reset board": "board fault", "board add": "where to add", "board delete": "no board at all", "reboot backplane": "impossible", "backplane abort": "install first", } match_cmd = [] match_rate = [] for line in sys.stdin: match_cmd.clear() match_rate.clear() cmd = line.strip().split() cmd_len = len(cmd) if len(cmd) > 2: print("unknown command") continue for k in commands.keys(): temp_match_cmd = [] temp_match_rate = [] kk = k.split() if len(kk) != cmd_len: continue for i in range(len(cmd)): pos = 0 for j in range(len(cmd[i])): if cmd[i][j] == kk[i][j]: pos += 1 continue else: break if pos == len(cmd[i]) and len(match_rate) > 0: if pos >= match_rate[i]: temp_match_cmd.append(kk[i]) temp_match_rate.append(pos) else: break elif pos == len(cmd[i]) and len(match_rate) == 0: temp_match_rate.append(pos) temp_match_cmd.append(kk[i]) elif pos == 0 or pos < len(cmd[i]): break if len(temp_match_rate) == len(cmd) and ( len(match_rate) == 0 or len(temp_match_cmd) == len(temp_match_rate) == len(match_rate) ): if temp_match_rate == match_rate: match_rate.clear() match_cmd.clear() break match_cmd = temp_match_cmd match_rate = temp_match_rate print(commands.get(" ".join(match_cmd), "unknown command"))