题解 | #配置文件恢复#

配置文件恢复

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"))

全部评论

相关推荐

06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
牛客nb666号:看数据范围, -1e4~1e4, 用一个计数数组存一下, 再按个数让k减到0就行; 堆排不是O(n)的, 快速选择算法是O(n)但随机性较强
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务