配置文件恢复
描述 |
有6条配置命令,它们执行的结果分别是:
注意:he he不是命令。 为了简化输入,方便用户,以“最短唯一匹配原则”匹配: 4、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果唯一,匹配成功。例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。
|
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
知识点 | 字符串 | ||||||||||||||||
运行时间限制 | 0M | ||||||||||||||||
内存限制 | 0 | ||||||||||||||||
输入 |
多行字符串,每行字符串一条命令 |
||||||||||||||||
输出 |
执行结果,每条命令输出一行 |
||||||||||||||||
样例输入 | reset reset board board add board delet reboot backplane backplane abort | ||||||||||||||||
样例输出 | reset what board fault where to add no board at all impossible install first |
package com.oj.test; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Test { private static String find(String str, Map<Integer, String> mapping, Map<Integer, String> base) { String[] input = str.split(" "); if(input.length==1&&"reset".startsWith(input[0])) return mapping.get(1); else if(input.length==2){ int[] flag = new int[7]; for(int i = 2;i <= 6; i++){ String[] test = base.get(i).split(" "); if(test[0].startsWith(input[0])&&test[1].startsWith(input[1])) flag[i] = 1; } int sum = 0; for(int i = 0;i < 7; i++) sum += flag[i]; if(sum==1){ for(int i = 0;i < 7; i++) if(flag[i]==1) return mapping.get(i); } } return mapping.get(7); } public static void main(String[] args) { Map<Integer,String> mapping = new HashMap<Integer,String>(); mapping.put(1, "reset what"); mapping.put(2, "board fault"); mapping.put(3, "where to add"); mapping.put(4, "no board at all"); mapping.put(5, "impossible"); mapping.put(6, "install first"); mapping.put(7, "unkown command"); Map<Integer,String> base = new HashMap<Integer,String>(); base.put(1, "reset"); base.put(2, "reset board"); base.put(3, "board add"); base.put(4, "board delet"); base.put(5, "reboot backplane"); base.put(6, "backplane abort"); Scanner in = new Scanner(System.in); String str; while((str=in.nextLine()) != null){ System.out.println(find(str,mapping,base)); } } }
态度决定高度,细节决定成败,