Didart

Post Office

Nov 17th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedHashMap;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class PostOffice {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String[] input = scanner.nextLine().split("\\s*\\|\\s*");
  12.         Matcher capitalLetters = Pattern.compile("([#$*&])(?<sym>[A-Z]+)\\1").matcher(input[0]);
  13.         Matcher infoWord = Pattern.compile("(?<ascii>[0-9]+):(?<length>[0-9]{2})").matcher(input[1]);
  14.         String[] words = input[2].split("\\s+");
  15.  
  16.         LinkedHashMap<Character, Integer> acceptedWords = new LinkedHashMap<>();
  17.  
  18.         String firstLetter = "";
  19.         while (capitalLetters.find())
  20.             firstLetter = capitalLetters.group("sym");
  21.  
  22.         for (int i = 0; i < firstLetter.length(); i++) {
  23.             acceptedWords.put(firstLetter.charAt(i), 0);
  24.         }
  25.  
  26.         while (infoWord.find()) {
  27.             char firstSymbol = (char) Integer.parseInt(infoWord.group("ascii"));
  28.             if (firstLetter.contains("" + firstSymbol)) {
  29.                 int length = Integer.parseInt(infoWord.group("length"))
  30.                         + 1;  
  31.                 if (length >= 1 && length <= 20)
  32.                     acceptedWords.put(firstSymbol, length);
  33.             }
  34.         }
  35.         acceptedWords
  36.                 .entrySet()
  37.                 .forEach((k -> {
  38.                     Arrays.stream(words)
  39.                             .filter(word -> word.charAt(0) == k.getKey() && word.length() == k.getValue())
  40.                             .forEach(System.out::println);
  41.                 }));
  42.  
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment