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;
- public class Events {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- Pattern pattern = Pattern.compile("^\\s*#([a-zA-Z]+)\\s*:\\s*@\\s*([a-zA-Z]+)\\s*(\\d+)\\s*:(\\d+)$");
- TreeMap<String,TreeMap<String, List<String>>> events = new TreeMap<>();
- for (int i = 0; i < num ; i++) {
- String inputLine = scanner.nextLine();
- Matcher matcher = pattern.matcher(inputLine);
- while (matcher.find()){
- String person = matcher.group(1);
- String place = matcher.group(2);
- String hour = "";
- String minutes = "";
- String time = "";
- if (Integer.parseInt(matcher.group(3)) <= 23 && Integer.parseInt(matcher.group(3)) >= 0){
- hour = matcher.group(3);
- }
- if (Integer.parseInt(matcher.group(4)) <= 59 && Integer.parseInt(matcher.group(4)) >= 0){
- minutes = matcher.group(4);
- }
- if (!hour.equals("") && !minutes.equals("")){
- time = String.format("%s:%s", hour, minutes);
- if (!events.containsKey(place)){
- events.put(place, new TreeMap<>());
- }
- if (!events.get(place).containsKey(person)){
- events.get(place).put(person, new LinkedList<>());
- }
- events.get(place).get(person).add(time);
- }
- }
- }
- String[] places = scanner.nextLine().split(",");
- Arrays.sort(places);
- int counter = 1;
- for (String place : places) {
- if (events.containsKey(place)){
- System.out.printf("%s:", place);
- System.out.println();
- for (Map.Entry<String, TreeMap<String, List<String>>> event : events.entrySet()) {
- event.getValue().entrySet().stream().forEach(entry -> {
- entry.getValue().sort((e1, e2) -> e1.compareTo(e2));
- System.out.printf("%d. %s -> %s", counter, entry.getKey(), String.join(", ", entry.getValue()));
- System.out.println();
- });
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement