题解 | #数据分类处理#

数据分类处理

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

import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {
            int indexOf = str.indexOf(" ");

            String[] s = stream(str.substring(indexOf).split(" ")).skip(1).toArray(
                             String[]::new);


            str = bf.readLine();
            int i = str.indexOf(" ");
            String[] s1 = str.substring(i).split(" ");
            Integer[] integers = stream(s1).distinct().skip(1).map(new
            Function<String, Integer>() {
                @Override
                public Integer apply(String s) {
                    return Integer.parseInt(s);
                }
            }).toArray(Integer[]::new);

            String[] keys = stream(integers).sorted().map(new Function<Integer, String>() {
                @Override
                public String apply(Integer integer) {
                    return integer + "";
                }
            }).toArray(String[]::new);

            HashMap<String, Integer> count = new HashMap<>();
            HashMap<String, String> index = new HashMap<>();
            int sum = 0;
            for (String key : keys) {
                count.put(key, 0);
                int temp = 0;
                for (int i1 = 0; i1 < s.length; i1++) {
                    if (s[i1].contains(key)) {
                        Integer integer = count.get(key);
                        integer++;
                        count.put(key, integer);
                        if (index.containsKey(key)) {
                            String index_k = index.get(key);
                            index_k = index_k + " " + i1 + " " + s[i1];
                            index.put(key, index_k);
                        } else {
                            index.put(key, i1 + " " + s[i1]);
                        }
                        temp = integer;
                    }
                }
                sum = sum + temp * 2;
            }

            for (String object : keys) {
                if (count.get(object) > 0)sum = sum + 2;
            }

            System.out.print(sum + " ");
            for (String object : keys) {
                Integer integer = count.get(object);
                if (integer > 0) {
                    Stream.of(object).forEach(System.out::print);
                    System.out.print(" " + integer + " ");
                    Stream.of(index.get(object)).forEach(System.out::print);
                    System.out.print(" ");
                }
            }

        }

    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 09:23
点赞 评论 收藏
分享
11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
11-24 19:04
已编辑
湖南工商大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务