题解 | #数据分类处理#

数据分类处理

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

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int I = in.nextInt();
            String[] inputs = new String[I];
            for (int i = 0; i < I; i++) {
                inputs[i] = in.next();
            }
            int R = in.nextInt();
            TreeSet<Integer> rules = new TreeSet<>();
            while (R-- > 0) {
                rules.add(in.nextInt());
            }
            ArrayList<Object> ans = new ArrayList<>();
            rules.forEach(rule -> {
                ArrayList<Object> temp = new ArrayList<>();
                for (int i = 0; i < inputs.length; i++) {
                    if (inputs[i].contains(rule.toString())) {
                        temp.add(i);
                        temp.add(inputs[i]);
                    }
                }
                if (temp.size() > 0) {
                    ans.add(rule);
                    ans.add(temp.size() / 2);
                    ans.addAll(temp);
                }
            });
            System.out.print(ans.size() + " ");
            ans.forEach(o -> System.out.print(o + " "));
        }
    }
}

全部评论

相关推荐

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