题解 | #数据分类处理#

数据分类处理

https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String s2 = scan.nextLine();
            String[] larr = s2.split(" ");
            String rs = scan.nextLine();
            String[] arrR = rs.split(" ");
            TreeSet<Integer> rSet = new TreeSet<Integer>();
            for (int i = 1; i < arrR.length; i++) {
                rSet.add(Integer.valueOf(arrR[i]));
            }
            //System.out.println("rSet:" + rSet);
            //序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)  按R<i>从小到大的顺序:rSet:[0, 3, 6]
            //
            TreeMap<Integer, Set<Integer>> aaa = new TreeMap<>();
            for (Integer i : rSet) {
                Set<Integer> suoyin = new TreeSet<Integer>();
                for (int j = 1; j < larr.length; j++) {
                    if (larr[j].contains(String.valueOf(i))) {
                        suoyin.add(j);
                        aaa.put(i, suoyin);
                    }
                }
            }
            //System.out.println(aaa);
            String res = "";
            int num =  aaa.keySet().size() * 2;
            for (Map.Entry<Integer, Set<Integer>> e : aaa.entrySet()) {
                Integer i = e.getKey();
                res += i + "," + e.getValue().size() + ",";
                num += e.getValue().size() * 2 ;
                for (Integer index : e.getValue()) {
                    res = res + (index - 1) + "," + larr[index] + ",";
                }

            }
            res = num + "," + res;
            System.out.println(res.substring(0, res.length() - 1).replaceAll(",", " "));
        }
    }
}

全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务