题解 | #数据分类处理#

数据分类处理

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());
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:00
林子大了什么鸟都有啊,我觉得我说的已经很客气了,阴阳谁呢
牛客62656195...:应该不是阴阳吧?你第一次注册的时候boss就说你是牛人
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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