public class Main{static Set<String> statesNeeds = new HashSet<>();static String[] str = {"mt", "wa", "or", "id", "nv", "ut", "ca", "az"};static Map<String,Set<String>> stations = new HashMap<>();//初始化public static void Init(){//需要覆盖的州for (int i = 0; i < str.length; i++) {statesNeeds.add(str[i]);}//广播站Set<String> temp = new HashSet<>();temp.add("id");temp.add("nv");temp.add("ut");stations.put("kone", temp);Set<String> temp1 = new HashSet<>();temp1.add("wa");temp1.add("id");temp1.add("mt");stations.put("ktwo", temp1);Set<String> temp2 = new HashSet<>();temp2.add("or");temp2.add("nv");temp2.add("ca");stations.put("kthree", temp2);Set<String> temp3 = new HashSet<>();temp3.add("nv");temp3.add("ut");stations.put("kfour", temp3);Set<String> temp4 = new HashSet<>();temp4.add("ca");temp4.add("az");stations.put("kfive", temp4);//System.out.println(stations);}public static void main(String[] args) {Init();List<String> finalStations = new LinkedList<>();while (statesNeeds.size()!=0) {Set<String> states_covered = new HashSet<>();String best_stations = null;for (Map.Entry<String,Set<String>> entry : stations.entrySet()) {Set<String> states_for_station = entry.getValue();states_for_station.retainAll(statesNeeds); //交集 coveredif (states_covered.size() < states_for_station.size()){best_stations = entry.getKey();states_covered = states_for_station;}}statesNeeds.removeAll(states_covered);finalStations.add(best_stations);}//结果为1.2.3.5 或者 2.3.4.5System.out.println(finalStations);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。