Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Race {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String[] participantsArray = sc.nextLine().split(", ");
- String participants = "";
- LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<>();
- for (int i = 0; i < participantsArray.length; i++) {
- //participants += participantsArray[i] + " ";
- linkedHashMap.putIfAbsent(participantsArray[i], 0);
- }
- String command;
- while (!"end of race".equals(command = sc.nextLine())) {
- StringBuilder name = new StringBuilder();
- int distance = 0;
- Pattern namePattern = Pattern.compile("[A-Za-z]");
- Pattern digitPattern = Pattern.compile("(?<digit>\\d)");
- Matcher nameMatcher = namePattern.matcher(command);
- Matcher digitMatcher = digitPattern.matcher(command);
- while (nameMatcher.find()) {
- name.append(nameMatcher.group());
- }
- while (digitMatcher.find()) {
- distance += Integer.parseInt(digitMatcher.group());
- }
- /* if (participants.contains(name)) {
- linkedHashMap.putIfAbsent(String.valueOf(name), distance);
- }
- */
- if (linkedHashMap.containsKey(name.toString())) {
- int oldDistance = linkedHashMap.get(name.toString());
- distance += oldDistance;
- linkedHashMap.put(name.toString(), distance);
- }
- }
- int[] counter = {1};
- linkedHashMap.entrySet().
- stream()
- .sorted((e1, e2) -> e2.getValue() - e1.getValue())
- .limit(3)
- .forEach(e -> {
- if (counter[0] == 1) {
- System.out.printf("1st place: %s%n", e.getKey());
- } else if (counter[0] == 2) {
- System.out.printf("2nd place: %s%n", e.getKey());
- } else {
- System.out.printf("3rd place: %s%n", e.getKey());
- }
- counter[0] = counter[0] + 1;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement