Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.TreeSet;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Events {
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- int lines = Integer.parseInt(scn.nextLine());
- Pattern pat = Pattern.compile("(#[a-zA-Z]+:)\\s*?(@[a-zA-Z]+)\\s*?(\\d+:\\d+)");
- TreeMap<String, TreeMap<String, ArrayList<String>>> locations = new TreeMap<>();
- while (lines > 0) {
- String line = scn.nextLine();
- Matcher match = pat.matcher(line);
- String location = "";
- String name = "";
- String time = "";
- if (match.find()) {
- name = match.group(1).substring(1, match.group(1).length() - 1);
- location = match.group(2).substring(1, match.group(2).length());
- time = match.group(3);
- }
- if ((location.equals("") || name.equals("")) || time.equals("")) {
- lines--;
- continue;
- }
- int hours = Integer.parseInt(time.substring(0,2));
- int minutes = Integer.parseInt(time.substring(3,5));
- if(hours>23||minutes>59){
- lines--;
- continue;
- }
- TreeMap<String, ArrayList<String>> names = new TreeMap<>();
- if (locations.containsKey(location)) {
- names = locations.get(location);
- if (!names.containsKey(name)) {
- names.put(name, new ArrayList<>());
- }
- names.get(name).add(time);
- } else {
- ArrayList<String> list = new ArrayList<>();
- list.add(time);
- names.put(name, list);
- locations.put(location, names);
- }
- lines--;
- }
- String[] line = scn.nextLine().split(",");
- TreeSet<String> demands = new TreeSet<>();
- for (int i = 0; i < line.length; i++) {
- demands.add(line[i]);
- }
- StringBuilder sb = new StringBuilder();
- for (String demand : demands) {
- if (locations.containsKey(demand)) {
- sb.append(demand + ":\n");
- TreeMap<String, ArrayList<String>> list = locations.get(demand);
- int count = 1;
- for (String name : list.keySet()){
- ArrayList<String> timeList = list.get(name);
- sb.append(String.format("%d. %s -> ",count,name,timeList.get(0)));
- timeList.stream().sorted().forEach(e -> sb.append(e + ", " ));
- sb.deleteCharAt(sb.length()-2);
- sb.append("\n");
- count++;
- }
- }
- }
- String result = sb.toString();
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement