题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
let temp; while(temp = readline()) { let res = macth(temp); if(res) { console.log(res); } else { console.log('unknown command'); } } function macth(str) { let arr = [ ['reset board', 'board fault'], ['board add', 'where to add'], ['board delete', 'no board at all'], ['reboot backplane', 'impossible'], ['backplane abort', 'install first'] ]; let brr = str.split(' '); if(brr.length == 1) { if('reset'.startsWith(brr[0])) { return 'reset what'; } } else { if( !'reset'.startsWith(brr[0]) && !'board'.startsWith(brr[0]) && !'reboot'.startsWith(brr[0]) && !'backplane'.startsWith(brr[0]) ) { return 'unknown command'; } else { let crr = []; for(let i = 0; i < arr.length; i++) { let drr = arr[i][0].split(' '); let l = 0; for(let k = 0; k < brr.length; k++) { if(drr[k].startsWith(brr[k])) { l++; } } if(l == brr.length) { crr.push(arr[i][1]); } } if(crr.length > 1) { return 'unknown command'; } else { return crr[0]; } } } }