Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package JavaFundamentalsExe;
  2.  
  3. import java.util.*;
  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 in = new Scanner(System.in);
  10. StringBuilder sb = new StringBuilder();
  11.  
  12. String[] arr = in.nextLine().split("\\|");
  13. String firstPart = arr[0];
  14. String secondPart = arr[1];
  15. String[] thirdPart = arr[2].split(" ");
  16.  
  17. String regex1 = "(?<first>\\#[A-Z]+\\#|\\$[A-Z]+\\$|\\*[A-Z]+\\*|\\&[A-Z]+\\&|\\%[A-Z]+\\%)";
  18. Pattern pattern1 = Pattern.compile(regex1);
  19. Matcher matcher = pattern1.matcher(firstPart);
  20.  
  21. Map<Character,Integer> letterLength = new LinkedHashMap<>();
  22. List<Character> capitalLetters = new ArrayList<>();
  23. while (matcher.find()) {
  24. String foundSequence = matcher.group("first");
  25. for (int i = 0; i < foundSequence.length(); i++) {
  26. char c = foundSequence.charAt(i);
  27. if (Character.isLetter(c)) {
  28. letterLength.putIfAbsent(c,0);
  29. }
  30. }
  31. }
  32.  
  33. String regex2 = "(?<secondFirstPart>[\\d]+):(?<secondSecondPart>[\\d][\\d])";
  34. Pattern pattern2 = Pattern.compile(regex2);
  35.  
  36. matcher = pattern2.matcher(secondPart);
  37.  
  38. while (matcher.find()) {
  39. int asciiValue = Integer.parseInt(matcher.group("secondFirstPart"));
  40. int length = Integer.parseInt(matcher.group("secondSecondPart"));
  41.  
  42. for (Map.Entry<Character, Integer> entry : letterLength.entrySet()) {
  43. int letter = (int) entry.getKey();
  44. if (asciiValue == letter){
  45. letterLength.put((char) letter,length+1);
  46. }
  47. }
  48. }
  49.  
  50. for (Map.Entry<Character, Integer> entry : letterLength.entrySet()) {
  51. char c = entry.getKey();
  52. int length = entry.getValue();
  53. for (int i = 0; i < thirdPart.length; i++) {
  54. char firstLetter = thirdPart[i].charAt(0);
  55. int lengthOfWord = thirdPart[i].length();
  56. if (c == firstLetter && length == lengthOfWord){
  57. sb.append(thirdPart[i] + "\n");
  58. }
  59. }
  60. }
  61.  
  62. System.out.println(sb.toString());
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement