Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Race {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // String[] names = sc.nextLine().split(", ");
- List<String> testNames = Arrays.stream(sc.nextLine().split(", ")).collect(Collectors.toList());
- String input = sc.nextLine();
- String regexName = "([A-Za-z]+)";
- Pattern namePattern = Pattern.compile(regexName);
- Matcher matcher = namePattern.matcher(input);
- String regexDistance = "([\\d])";
- Pattern distancePattern = Pattern.compile(regexDistance);
- Map<String, Integer> results = new LinkedHashMap<>();
- while (!input.equals("end of race")) {
- String currentName = "";
- while (matcher.find()) {
- currentName = currentName + matcher.group(1);
- }
- if (testNames.contains(currentName)) {
- matcher = distancePattern.matcher(input);
- int newResult = 0;
- while (matcher.find()) {
- newResult += Integer.parseInt(matcher.group(1));
- }
- if (results.containsKey(currentName)) {
- int oldResult = results.get(currentName);
- results.put(currentName, oldResult + newResult);
- } else {
- results.put(currentName, newResult);
- }
- }
- input = sc.nextLine();
- matcher = namePattern.matcher(input);
- }
- // for (Map.Entry<String, Integer> entry : results.entrySet()) {
- // System.out.printf("%s - %d%n", entry.getKey(), entry.getValue());
- // }
- List<String> finals = new ArrayList<>();
- results.entrySet().stream().sorted((a, b) ->{
- int result = b.getValue() - a.getValue();
- return result;
- })
- .limit(3)
- .forEach(e -> finals.add(e.getKey()));
- System.out.printf("1st place: %s%n", finals.get(0));
- System.out.printf("2nd place: %s%n", finals.get(1));
- System.out.printf("3rd place: %s%n", finals.get(2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement