Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package A_TheExam;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class P03 {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(reader.readLine());
- List<String> attackerList = new ArrayList<>();
- List<String> destroyList = new ArrayList<>();
- String message = "";
- int newLetter = 0;
- String newStr = "";
- for (int i = 0; i < n; i++) {
- message = reader.readLine();
- int countLetters = 0;
- for (int j = 0; j < message.length(); j++) {
- //char test = message.charAt(j);
- if (message.charAt(j) == 's' ||
- message.charAt(j) == 't' ||
- message.charAt(j) == 'a' ||
- message.charAt(j) == 'r' ||
- message.charAt(j) == 'S' ||
- message.charAt(j) == 'T' ||
- message.charAt(j) == 'A' ||
- message.charAt(j) == 'R') {
- countLetters++;
- }
- }
- StringBuilder sb = new StringBuilder();
- for (int j = 0; j < message.length(); j++) {
- newLetter = message.charAt(j) - countLetters;
- StringBuilder newStrBuild = sb.append((char) newLetter);
- newStr = newStrBuild.toString();
- }
- String regex = "[^@\\-\\:\\!,\\>]*@([A-Za-z]+)" +
- "[^@\\-\\:\\!,\\>]*\\:([0-9]+)" +
- "[^@\\-\\:\\!,\\>]*\\!([AD])\\!" +
- "[^@\\-\\:\\!,\\>]*(->)([0-9]+)";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(newStr);
- while (matcher.find()) {
- String type = matcher.group(3);
- String name = matcher.group(1);
- if (type.equals("A")){
- attackerList.add(name);
- Collections.sort(attackerList);
- } else if (type.equals("D")) {
- destroyList.add(name);
- Collections.sort(destroyList);
- }
- }
- }
- System.out.printf("Attacked planets: %d%n", attackerList.size());
- attackerList
- .stream()
- .sorted((s1,s2)->s1.compareTo(s2))
- .forEach(s->System.out.printf("-> %s%n", s));
- System.out.printf("Destroyed planets: %d%n", destroyList.size());
- destroyList
- .stream()
- .sorted((s1,s2)->s1.compareTo(s2))
- .forEach(s->System.out.printf("-> %s%n", s));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment