1. 程式人生 > 其它 >牛客華為機試HJ66

牛客華為機試HJ66

原題傳送門

1. 問題描述

2. Solution

import re
import sys

if sys.platform != "linux":
    file_in = open("input/HJ66.txt")
    sys.stdin = file_in

command_map = {
    # ("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"
}

"""
r b
b a
bo a
reb
r
res

unknown command
unknown command
where to add
unknown command
reset what
reset what
"""


def solve(s):
    parts = s.split()
    matched_keys = []
    if len(parts) == 1:
        if "reset".startswith(s):
            print("reset what")
        else:
            print("unknown command")
        return
    for k, v in command_map.items():
        p1, p2 = parts
        if k[0].startswith(p1) and k[1].startswith(p2):
            matched_keys.append(k)
    if not matched_keys or len(matched_keys) > 1:
        print("unknown command")
        return
    print(command_map[matched_keys[0]])


for line in sys.stdin:
    s = line.strip()
    solve(s)