题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static final Map<Command, String> ONE_KEYWORD_COMMAND_TO_RESULT = new
LinkedHashMap<>();
public static final Map<Command, String> TWO_KEYWORD_COMMAND_TO_RESULT = new
LinkedHashMap<>();
public static final String UNKNOWN_COMMAND = "unknown command";
static {
ONE_KEYWORD_COMMAND_TO_RESULT.put(new Command(1, "reset"), "reset what");
TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "reset", "board"),
"board fault");
TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "board", "add"),
"where to add");
TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "board", "delete"),
"no board at all");
TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "reboot", "backplane"),
"impossible");
TWO_KEYWORD_COMMAND_TO_RESULT.put(new Command(2, "backplane", "abort"),
"install first");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String commandString = in.nextLine();
String result = null;
Command command = getCommand(commandString);
String value1 = ONE_KEYWORD_COMMAND_TO_RESULT.get(command);
String value2 = TWO_KEYWORD_COMMAND_TO_RESULT.get(command);
if (value1 != null) {
result = value1;
} else if (value2 != null) {
result = value2;
} else {
int keywordCount = command.keywordCount;
if (keywordCount == 1) {
Set<Command> commands = ONE_KEYWORD_COMMAND_TO_RESULT.keySet();
for (Command c : commands) {
if (c.getValue1().contains(command.getValue1())) {
result = ONE_KEYWORD_COMMAND_TO_RESULT.get(c);
}
}
if (result == null) {
result = UNKNOWN_COMMAND;
}
}
if (keywordCount == 2) {
Set<Command> commands = TWO_KEYWORD_COMMAND_TO_RESULT.keySet();
Set<Command> matchedCommands = new LinkedHashSet<>();
for (Command c : commands) {
if (c.getValue1().indexOf(command.getValue1()) == 0) {
matchedCommands.add(c);
}
}
int count = 0;
String firstKeyword = null;
String secondKeyword = null;
if (!matchedCommands.isEmpty()) {
for (Command c : matchedCommands) {
if (c.getValue2().indexOf(command.getValue2()) == 0) {
count++;
firstKeyword = c.value1;
secondKeyword = c.value2;
}
}
if (count != 1) {
result = UNKNOWN_COMMAND;
} else {
Command c = new Command(2, firstKeyword, secondKeyword);
result = TWO_KEYWORD_COMMAND_TO_RESULT.get(c);
}
} else {
result = UNKNOWN_COMMAND;
}
}
}
System.out.println(result);
}
in.close();
}
public static Command getCommand(String command) {
String[] strings = command.split(" ");
int length = strings.length;
if (length == 2) {
return new Command(length, strings[0], strings[1]);
} else {
return new Command(length, strings[0]);
}
}
public static class Command {
private int keywordCount;
private String value1;
private String value2;
public Command(int keywordCount, String value1, String value2) {
this.keywordCount = keywordCount;
this.value1 = value1;
this.value2 = value2;
}
public Command(int keywordCount, String value1) {
this.keywordCount = keywordCount;
this.value1 = value1;
}
public int getKeywordCount() {
return keywordCount;
}
public void setKeywordCount(int keywordCount) {
this.keywordCount = keywordCount;
}
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Command command = (Command) o;
return Objects.equals(value1, command.value1)
&& Objects.equals(value2, command.value2);
}
@Override
public int hashCode() {
return Objects.hash(value1, value2);
}
}
}

