题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
Golang题解
package main
import (
"fmt"
"bufio"
"os"
"strings"
)
var output = map[string]string {
"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",
}
var cmds = [][]string {
[]string{"reset"},
[]string{"reset", "board"},
[]string{"board", "add"},
[]string{"board", "delete"},
[]string{"reboot", "backplane"},
[]string{"backplane", "abort"},
}
func main() {
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
cmd := sc.Text()
subCmd := strings.Fields(cmd)
switch len(subCmd) {
case 1:
if strings.HasPrefix("reset", cmd) {
fmt.Println(output["reset"])
} else {
fmt.Println("unknown command")
}
case 2:
index := -1
for i := 1; i < 6; i++ {
if strings.HasPrefix(cmds[i][0], subCmd[0]) && strings.HasPrefix(cmds[i][1], subCmd[1]) {
if index != -1 {
index = -1
break
}
index = i
}
}
if index != -1 {
fmt.Println(output[strings.Join(cmds[index], " ")])
} else {
fmt.Println("unknown command")
}
default:
fmt.Println("unknown command")
}
}
}