题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
//1.s
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
//立即执行函数
//就把命令按长度排序 (题目里已排好),然后正则表达式匹配,以开头匹配
let inputs = [
"reset",
"reset board",
"board add",
"board delete",
"reboot backplane",
"backplane abort",
];
let outputs = [
"reset what",
"board fault",
"where to add",
"no board at all",
"impossible",
"install first",
"unknown command",
];
void (async function () {
// Write your code here
while ((line = await readline())) {
let input = line.split(" ");
if (input.length == 1) {
let str = "^" + input[0];
let re = new RegExp(str);
if (re.test(inputs[0])) {
console.log(outputs[0]);
} else {
console.log(outputs[6]);
}
} else if (input.length == 2) {
let str1 = "^" + input[0];
let str2 = "^" + input[1];
// ]标记当前是否匹配
let flag = false;
let count = 0;
let res = "";
for (let i = 1; i < 6; i++) {
let commands = inputs[i].split(" ");
let com1 = commands[0];
let com2 = commands[1];
let re1 = new RegExp(str1);
let re2 = new RegExp(str2);
if (re1.test(com1)) {
if (re2.test(com2)) {
count++;
res = outputs[i];
flag = true;
}
}
}
if (flag && count == 1) {
console.log(res);
} else {
console.log(outputs[6]);
}
}
}
})();