Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _12_StarEnigma {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- String reg =
- "@(?<name>[A-Za-z]+)(?>[^@:!\\->]*):(?<population>\\d+)(?>[^@:!\\->]*)!(?<attack>[AD])!(?>[^@:!\\->]*)->(?<soldiers>\\d+)";
- Pattern p = Pattern.compile(reg);
- List<String> infoA = new ArrayList<>();
- List<String> infoD = new ArrayList<>();
- String line = "";
- for (int i = 0; i < n; i++) { //for each line
- line = scanner.nextLine();
- int key = 0;
- StringBuilder updatedLine = new StringBuilder();
- for (int j = 0; j < line.length(); j++) { //along the line length counting
- if (line.charAt(j) == 's' || line.charAt(j) == 't'
- || line.charAt(j) == 'a' || line.charAt(j) == 'r'
- || line.charAt(j) == 'S' || line.charAt(j) == 'T'
- || line.charAt(j) == 'A' || line.charAt(j) == 'R') {
- key++;
- }
- }
- //Update characters:
- for (int k = 0; k < line.length(); k++) {
- char charche = (char) ((int) line.charAt(k) - (key));
- updatedLine.append(charche);
- }
- Matcher m = p.matcher(updatedLine);
- if (m.find()) {
- String name = m.group("name");
- String attack = m.group("attack");
- if (attack.equals("A")) {
- infoA.add(name);
- } else {
- infoD.add(name);
- }
- }
- }
- System.out.println("Attacked planets: " + infoA.size());
- Collections.sort(infoA);
- for (String s : infoA) {
- System.out.println("-> " + s);
- }
- System.out.println("Destroyed planets: " + infoD.size());
- Collections.sort(infoD);
- for (String s : infoD) {
- System.out.println("-> " + s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement