题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("reset","reset what");
map.put("reset board","board fault");
map.put("board add","where to add");
map.put("board delete","no board at all");
map.put("reboot backplane","impossible");
map.put("backplane abort","install first");
Set<String> keys = map.keySet();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String str = scanner.nextLine();
String[] receive = str.split(" ");
List<String> list = new ArrayList<>();
List<String> errList = new ArrayList<>();
for (String value : keys) {
String[] key = value.split(" ");
if(receive.length==2){
if(key.length==2){
if(key[0].startsWith(receive[0])&&key[1].startsWith(receive[1])){
list.add(value);
continue;
}
}else{
continue;
}
}else{
if(key.length==1){
if(key[0].startsWith(receive[0])){
list.add(value);
continue;
}
}else if(key[0].startsWith(receive[0])){
errList.add(value);
continue;
}
}
}
if(list.size()!=0){
if(list.size()>1){
System.out.println("unknown command");
}else{
System.out.println(map.get(list.get(0)));
}
}else if(errList.size()>0){
System.out.println("unknown command");
}else{
System.out.println("unknown command");
}
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("reset","reset what");
map.put("reset board","board fault");
map.put("board add","where to add");
map.put("board delete","no board at all");
map.put("reboot backplane","impossible");
map.put("backplane abort","install first");
Set<String> keys = map.keySet();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String str = scanner.nextLine();
String[] receive = str.split(" ");
List<String> list = new ArrayList<>();
List<String> errList = new ArrayList<>();
for (String value : keys) {
String[] key = value.split(" ");
if(receive.length==2){
if(key.length==2){
if(key[0].startsWith(receive[0])&&key[1].startsWith(receive[1])){
list.add(value);
continue;
}
}else{
continue;
}
}else{
if(key.length==1){
if(key[0].startsWith(receive[0])){
list.add(value);
continue;
}
}else if(key[0].startsWith(receive[0])){
errList.add(value);
continue;
}
}
}
if(list.size()!=0){
if(list.size()>1){
System.out.println("unknown command");
}else{
System.out.println(map.get(list.get(0)));
}
}else if(errList.size()>0){
System.out.println("unknown command");
}else{
System.out.println("unknown command");
}
}
}
}