Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package Lecture9Regex.exersice;
  2.  
  3.  
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class Star {
  11. public static void main(String[] args) {
  12. Scanner scanner = new Scanner(System.in);
  13.  
  14. Map<String, Integer> atackerPlanet = new TreeMap<>();
  15. Map<String, Integer> defencePlanet = new TreeMap<>();
  16.  
  17. int n = Integer.parseInt(scanner.nextLine());
  18. for (int i = 0; i < n; i++) {
  19. String operation = scanner.nextLine();
  20. String regex = "[STARstar]+";
  21. Pattern pattern = Pattern.compile(regex);
  22. Matcher matcher = pattern.matcher(operation);
  23. int count = 0;
  24. while (matcher.find()) {
  25. count++;
  26. }
  27. String currentData = "";
  28. for (int j = 0; j < operation.length(); j++) {
  29. char letter = (char) (operation.charAt(j) - count);
  30. currentData += letter;
  31. }
  32.  
  33. String planetRegex = "@([A-Z][a-z]+)[^@\\-!:>]*\\d?[^@\\-!:>]*:(\\d+)!(A|D)!->[^@\\-!:>]*?(\\d+)";
  34. Pattern pattern1 = Pattern.compile(planetRegex);
  35. Matcher matcher1 = pattern1.matcher(currentData);
  36.  
  37. while (matcher1.find()) {
  38. String planetName = matcher1.group(1);
  39. String aOrD = matcher1.group(3);
  40.  
  41. if (aOrD.equals("A")) {
  42. if (!atackerPlanet.containsKey(planetName)) {
  43. atackerPlanet.put(planetName, 1);
  44. }
  45. } else if (aOrD.equals("D")) {
  46. if (!defencePlanet.containsKey(planetName)) {
  47. defencePlanet.put(planetName, 1);
  48. }
  49. }
  50. }
  51. }
  52. System.out.printf("Attacked planets: %d\n", atackerPlanet.values().size());
  53. atackerPlanet.forEach((key, value) -> System.out.printf("-> %s\n", key));
  54. System.out.printf("Destroyed planets: %d\n", defencePlanet.values().size());
  55. defencePlanet.forEach((key, value) -> System.out.printf("-> %s\n", key));
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement