kalinikov

04. Hornet Armada - 60/100 - no regex

Dec 4th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class HornetArmada {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. int n = Integer.parseInt(scanner.nextLine());
  12.  
  13. Map<String, Integer> nameActivity = new LinkedHashMap<>();
  14. Map<String, Map<String, Long>> nameTypeCount = new LinkedHashMap<>();
  15.  
  16. for (int i = 0; i < n; i++) {
  17. String info = scanner.nextLine();
  18.  
  19. /*String regex = "^(?<activity>\\d+) = (?<name>[^=->: ]+) -> (?<type>[^=->: ]+):(?<count>\\d+)$";
  20. Pattern pattern = Pattern.compile(regex);
  21. Matcher matcher = pattern.matcher(info);*/
  22.  
  23. //if (matcher.find()) {
  24. int activity = Integer.parseInt(info.split(" = ")[0]);
  25. String temp1 = info.split(" = ")[1];
  26. String name = temp1.split(" -> ")[0];
  27. String temp2 = temp1.split(" -> ")[1];
  28. String type = temp2.split(":")[0];
  29. long count = Long.parseLong(temp2.split(":")[1]);
  30.  
  31. if (!nameActivity.containsKey(name)) {
  32. nameActivity.put(name, activity);
  33. nameTypeCount.put(name, new LinkedHashMap<>());
  34. nameTypeCount.get(name).put(type, count);
  35. } else {
  36. if (activity > nameActivity.get(name)) {
  37. nameActivity.put(name, activity);
  38. }
  39. if (!nameTypeCount.get(name).containsKey(type)) {
  40. nameTypeCount.get(name).put(type, count);
  41. } else {
  42. nameTypeCount.get(name).put(type, count + nameTypeCount.get(name).get(type));
  43. }
  44. }
  45. //}
  46. }
  47.  
  48. String line = scanner.nextLine();
  49.  
  50. if (line.contains("\\")) {
  51. int activity = Integer.parseInt(line.split("\\\\")[0]);
  52. String soldierType = line.split("\\\\")[1];
  53.  
  54. if (!nameTypeCount.isEmpty()) {
  55. nameTypeCount.entrySet()
  56. .stream()
  57. .filter(e -> {
  58. return nameActivity.get(e.getKey()) < activity;
  59. })
  60. .sorted((f, s) -> {
  61. return (int) (s.getValue().get(soldierType) - f.getValue().get(soldierType));
  62. })
  63. .forEach(e -> {
  64. System.out.printf("%s -> %d%n", e.getKey(), e.getValue().get(soldierType));
  65. });
  66. }
  67.  
  68. } else {
  69. String soldierType = line;
  70.  
  71. if (!nameActivity.isEmpty()) {
  72. nameActivity.entrySet()
  73. .stream()
  74. .filter(e -> {
  75. return nameTypeCount.get(e.getKey()).containsKey(soldierType);
  76. })
  77. .sorted((f, s) -> {
  78. return s.getValue() - f.getValue();
  79. })
  80. .forEach(e -> {
  81. System.out.printf("%d : %s%n", e.getValue(), e.getKey());
  82. });
  83. }
  84. }
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment