Guest User

JavaFundamentals28022016Events

a guest
Mar 26th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. /*
  6.  * Created by MapuH on 26-Mar-2016
  7.  *
  8.  * Description can be found at:
  9.  * https://judge.softuni.bg/Contests/Practice/DownloadResource/1181
  10.  *
  11.  * NOTE: Gets only 80% score on the Open Judge System, waiting for tests to check and fix.
  12.  */
  13. public class Events {
  14.  
  15.     private static boolean isValidTime(String time) {
  16.         int separator = time.indexOf(":");
  17.         int hour = Integer.parseInt(time.substring(0, separator));
  18.         int minutes = Integer.parseInt(time.substring(separator+1));
  19.  
  20.         return hour <= 23 && minutes <= 59;
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.  
  25.         Scanner input = new Scanner(System.in);
  26.         int n = Integer.parseInt(input.nextLine());
  27.  
  28.         Map<String, Map<String, List<String>>> eventMap = new HashMap<>();
  29.  
  30.         Pattern pattern = Pattern.compile("(#[a-zA-Z]+:)\\s*?(@[a-zA-Z]+)\\s*?(\\d+:\\d+)");
  31.         for (int i = 0; i < n; i++) {
  32.             String line = input.nextLine().trim();
  33.             Matcher match = pattern.matcher(line);
  34.             while (match.find()) {
  35.                 String person = match.group(1);
  36.                 String location = match.group(2);
  37.                 String hour = match.group(3);
  38.  
  39.                 person = person.substring(1, person.length()-1);
  40.                 location = location.substring(1);
  41.  
  42.                 if (!isValidTime(hour)) {
  43.                     break;
  44.                 }
  45.  
  46.                 if (!eventMap.containsKey(location)) {
  47.                     List<String> times = new ArrayList<>();
  48.                     times.add(hour);
  49.                     Map<String, List<String>> nameMap = new TreeMap<>();
  50.                     nameMap.put(person, times);
  51.                     eventMap.put(location, nameMap);
  52.  
  53.                 } else {
  54.                     Map<String, List<String>> nameMap = eventMap.get(location);
  55.                     if (!nameMap.containsKey(person)) {
  56.                         List<String> times = new ArrayList<>();
  57.                         times.add(hour);
  58.                         nameMap.put(person, times);
  59.                     } else {
  60.                         List<String> times = nameMap.get(person);
  61.                         times.add(hour);
  62.                         Collections.sort(times);
  63.                         nameMap.put(person, times);
  64.                     }
  65.  
  66.                     eventMap.put(location, nameMap);
  67.                 }
  68.  
  69.             }
  70.         }
  71.  
  72.         String[] locations = input.nextLine().split(",");
  73.         Arrays.sort(locations);
  74.  
  75.         for (String location : locations) {
  76.             if (!eventMap.containsKey(location)) {
  77.                 continue;
  78.             }
  79.  
  80.             System.out.println(location + ":");
  81.  
  82.             int number = 1;
  83.             for (Map.Entry<String, List<String>> nameEntry : eventMap.get(location).entrySet()) {
  84.                 String times = nameEntry.getValue().toString();
  85.                 System.out.printf("%d. %s -> %s\n", number, nameEntry.getKey(), times.substring(1, times.length()-1));
  86.                 number++;
  87.             }
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment