题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

package main

//建立一个map叫做command依次存放所有存在的命令组合,
//如果这个key存在 则重置为unknow 例"b a"第一次存放时command["b a"]="where to add"
//第二次在“backplane abort”也会存在"b a"命令,此时将command["b a"]="unknown command"
//读取屏幕输入以回车结束
import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	command := make(map[string]string)
	un := "unknown command"
	initMap("reset", "", "reset what", un, command)
	initMap("reset", "board", "board fault", un, command)
	initMap("board", "add", "where to add", un, command)
	initMap("board", "delete", "no board at all", un, command)
	initMap("reboot", "backplane", "impossible", un, command)
	initMap("backplane", "abort", "install first", un, command)

	sc := bufio.NewScanner(os.Stdin)
	for sc.Scan() {
		cmd := sc.Text()
        //判断cmd命令是否存在于map
		todo, ok := command[cmd]
		if ok {
			fmt.Println(todo)
		} else {
			fmt.Println(un)
		}
	}

}

func initMap(first string, second string, todo string, unknow string, comd map[string]string) {
	if len(second) == 0 {
		for i := 0; i < len(first); i++ {
			comd[first[:i+1]] = todo
		}
	} else {
		for i := 0; i < len(first); i++ {
			for j := 0; j < len(second); j++ {
				if _, ok := comd[first[:i+1]+" "+second[:j+1]]; !ok {
					comd[first[:i+1]+" "+second[:j+1]] = todo
				} else {
					comd[first[:i+1]+" "+second[:j+1]] = unknow
				}
			}
		}
	}
}


全部评论

相关推荐

09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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