题解 | #数据分类处理#treemap存储出现的位置和值
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int rn = sc.nextInt(); String[] st = new String[rn]; for(int i=0;i<rn;i++){ st[i] =sc.next(); } int jn = sc.nextInt(); TreeSet<Integer> set = new TreeSet<Integer>(); for(int i=0;i< jn;i++){ set.add(sc.nextInt()); } //用treemap存储索引和值就可以了 StringBuffer str = new StringBuffer(); Iterator<Integer> iterator = set.iterator(); for(int num:set){ String e = String.valueOf(num); str.append(getRes(e,st)); } System.out.println(str.toString().split(" ").length+" "+str); } public static StringBuffer getRes(String e,String[] st){ StringBuffer res = new StringBuffer(); TreeMap<Integer,String> map = new TreeMap<Integer, String>(); for(int i=0;i<st.length;i++){ if(st[i].contains(e)){ map.put(i,st[i]); } } if(map.size()>0) res.append(e+" "+map.size()+" "); for(Map.Entry entry : map.entrySet()){ res.append(entry.getKey()+" "+entry.getValue()+" "); } return res; } }