Tsuki11

PostOffice

Mar 28th, 2020
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class MoreExercise_3_PostOffice {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.  
  9.         String input = scan.nextLine();
  10.         String[] tokens = input.split("\\|");
  11.         String first = tokens[0];
  12.         String numbers = tokens[1];
  13.         String words = tokens[2];
  14.  
  15.         Pattern patternMessage = Pattern.compile("([#$%*&])([A-Z]+)\\1");
  16.         Matcher match = patternMessage.matcher(first);
  17.  
  18.         String capitalLetters = "";
  19.         while (match.find()) {
  20.             String current = match.group();
  21.             capitalLetters = current.substring(1, current.length() - 1);
  22.  
  23.         }
  24.  
  25.         Map<Character, Integer> collectionCharacteristics = new LinkedHashMap<>();
  26.  
  27.         for (int i = 0; i < capitalLetters.length(); i++) {
  28.             int asciiValue = capitalLetters.charAt(i);
  29.  
  30.             String regexDigits = "([0-9]{2}):([0-9]{2})";
  31.             Pattern patternDigits = Pattern.compile(regexDigits);
  32.             Matcher matchDigits = patternDigits.matcher(numbers);
  33.  
  34.             while (matchDigits.find()) {
  35.                 int number = Integer.parseInt(matchDigits.group(1));
  36.                 int length = Integer.parseInt(matchDigits.group(2));
  37.                 if (asciiValue == number) {
  38.                     char letter = (char) number;
  39.                     collectionCharacteristics.putIfAbsent(letter, length + 1);
  40.                     break;
  41.                 }
  42.             }
  43.  
  44.         }
  45.  
  46.         for (int i = 0; i < capitalLetters.length(); i++) {
  47.             String letter = String.valueOf(capitalLetters.charAt(i));
  48.             int legth = collectionCharacteristics.get(capitalLetters.charAt(i));
  49.  
  50.             String patternCurrent = String.format("\\b(.?[%s][a-zA-z0-9_-]{%d})\\b", letter, legth-1);
  51.             Pattern patternCurr = Pattern.compile(patternCurrent);
  52.  
  53.             Matcher newMatch = patternCurr.matcher(words);
  54.             while (newMatch.find()) {
  55.                 String current = newMatch.group().trim();
  56.                 if (!Character.isAlphabetic(current.charAt(0))) {
  57.                     continue;
  58.                 }
  59.                 System.out.println(current);
  60.  
  61.             }
  62.  
  63.         }
  64.     }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment