题解 | #HJ025 数据分类处理#

数据分类处理

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

import java.util.*;

/**
 * HJ25 数据分类处理
 */
public class HJ025 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            String[] arrI = new String[m];
            for (int i = 0; i < m; i++) {
                arrI[i] = sc.next();
            }
            int n = sc.nextInt();
            int[] arrR = new int[n];
            for (int i = 0; i < n; i++) {
                arrR[i] = sc.nextInt();
            }
            System.out.println(suoyin(arrI, arrR));
        }
        sc.close();
    }

    private static String suoyin(String[] arrI, int[] arrR) {
        Arrays.sort(arrR); // 将序列R排序
        String res = "";
        int sum = 0;
        for (int i = 0; i < arrR.length; i++) {
            if (i == 0 || arrR[i] != arrR[i - 1]) {
                int count = 0;
                String s = "";
                String str = String.valueOf(arrR[i]);
                for (int j = 0; j < arrI.length; j++) {
                    if (arrI[j].contains(str)) {
                        count++;
                        s = s + j + " " + arrI[j] + " ";
                    }
                }
                if (count > 0) {
                    sum = sum + count;
                    res = res + arrR[i] + " " + count + " " + s;
                }
            }

        }
        String[] str = res.split(" ");
        res = str.length + " " + res;
        return res;
    }
}
全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
09-24 18:30
已编辑
长春工业大学 产品经理
小肥罗:HR就是好人的缩写哈哈哈哈
点赞 评论 收藏
分享
评论
5
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务