3 条回复
package xiaozhao; import java.util.*; public class t2 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>(); int i = 0; boolean flag = false; for (int j = 0; j < n; j++) { String str = scanner.nextLine(); String s[] = str.split(" "); if (map.containsKey(s[0])) { ArrayList<String> list = map.get(s[0]); list.add(s[1]); map.put(s[0],list); } else { ArrayList<String> stringList = new ArrayList<String>(); stringList.add(s[1]); map.put(s[0], stringList); } } for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) { if (entry.getValue().size() > 1) { flag = true; StringBuffer stringBuffer = new StringBuffer(); for (String s : entry.getValue()) { stringBuffer.append(s + " "); } System.out.println(entry.getKey() + " " + stringBuffer.toString()); } } if (flag == false) { System.out.println("YES"); } } }
添加回复