Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class kur {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, Integer> dwarfs = new LinkedHashMap<>();
  8.         Map<String, Integer> dwarfCountByColor = new LinkedHashMap<>();
  9.         String input;
  10.  
  11.         while (!"Once upon a time".equals(input = scanner.nextLine())) {
  12.  
  13.             String[] lineInput = input.split(" <:> ");
  14.  
  15.             String dwarf = lineInput[1] + " " + lineInput[0];
  16.             String color = lineInput[1];
  17.             int physics = Integer.parseInt(lineInput[2]);
  18.  
  19.             dwarfCountByColor.putIfAbsent(color, 0);
  20.             dwarfCountByColor.put(color, dwarfCountByColor.get(color) + 1);
  21.  
  22.             if (dwarfs.containsKey(dwarf)) {
  23.                 if (dwarfs.get(dwarf) < physics) {
  24.                     dwarfs.put(dwarf, physics);
  25.                     dwarfCountByColor.put(color, dwarfCountByColor.get(color) - 1);
  26.                 }
  27.             }else {
  28.                 dwarfs.put(dwarf, physics);
  29.             }
  30.         }
  31.  
  32.         dwarfs.entrySet().stream().sorted((pair1, pair2) -> {
  33.             int sort = Integer.compare(pair2.getValue(), pair1.getValue());
  34.             if (sort == 0) {
  35.  
  36.                 String[] color1 = pair1.getKey().split(" ");
  37.                 String[] color2 = pair2.getKey().split(" ");
  38.  
  39.                 int size1 = dwarfCountByColor.get(color1[0]);
  40.                 int size2 = dwarfCountByColor.get(color2[0]);
  41.                 sort = Integer.compare(size2, size1);
  42.             }
  43.             return sort;
  44.         }).forEach(pair -> {
  45.             String[] print = pair.getKey().split(" ");
  46.             System.out.printf("(%s) %s <-> %d\n", print[0], print[1], pair.getValue());
  47.         });
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement