题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.util.*; import java.util.function.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String ri = in.nextLine(); String r = in.nextLine(); String[] rs = r.split(" "); String[] ris = ri.split(" "); Set<Integer> treeSet = new TreeSet(); for (int i = 1; i < rs.length; i++) { treeSet.add(Integer.parseInt(rs[i])); } StringBuilder sb = new StringBuilder(); int count = 0; String tempStr = ""; for (Integer o : treeSet) { for (int i = 1; i < ris.length; i++) { if (ris[i].contains(o + "")) { count++; tempStr = tempStr + (i - 1) + " " + ris[i] + " "; } } if (count > 0) { tempStr = o + " " + count + " " + tempStr; } sb.append(tempStr); count = 0;//复位,重新统计 tempStr = ""; } int len = sb.toString().split(" ").length; System.out.println(len + " " + sb.toString()); } } }