Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
3
Indexable
Never
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class JudgeExercise_09 {

    public static void printingMap(LinkedHashMap<String, ArrayList<String>> map) {
        for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
            String forceSide = entry.getKey();
            int numberOfUsers = entry.getValue().size();
            if (numberOfUsers > 0) {
                System.out.printf("Side: %s, Members: %d\n", forceSide, numberOfUsers);
                printingList(entry.getValue());
            }
        }
    }

    public static void printingList(ArrayList<String> users) {
        for (String user : users) {
            System.out.printf("! %s\n", user);
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        LinkedHashMap<String, String> usersAndSides = new LinkedHashMap<>();
        LinkedHashMap<String, ArrayList<String>> forceBook = new LinkedHashMap<>();

        while (true) {
            String text = sc.nextLine();
            if (text.equals("Lumpawaroo")) break;

            String[] textParts = text.split(" ");
            int indexDelimiter;

            for (int i = 0; i < textParts.length; i++) {
                if (textParts[i].equals("|")) {
                    indexDelimiter = i;
                    String side = "";
                    for (int j = 0; j < indexDelimiter; j++) {
                        side = side + " " + textParts[j];
                        side = side.trim();
                    }
                    String user = "";
                    for (int j = indexDelimiter + 1; j < textParts.length; j++) {
                        user = user + " " + textParts[j];
                        user = user.trim();
                    }


                    if (!usersAndSides.containsKey(user)) {
                        usersAndSides.put(user, side);
                    } else continue;

                    if (!forceBook.containsKey(side)) {
                        forceBook.put(side, new ArrayList<>());
                        forceBook.get(side).add(user);
                    }
                }
            }

            for (int i = 0; i < textParts.length; i++) {
                if (textParts[i].equals("->")) {
                    indexDelimiter = i;
                    String user = "";
                    for (int j = 0; j < indexDelimiter; j++) {
                        user = user + " " + textParts[j];
                        user = user.trim();
                    }
                    String side = "";
                    for (int j = indexDelimiter+1; j < textParts.length; j++) {
                        side = side + " " + textParts[j];
                        side = side.trim();
                    }

                    if (!usersAndSides.containsKey(user) && !forceBook.containsKey(side)) {
                        usersAndSides.put(user, side);
                        forceBook.put(side, new ArrayList<>());
                        forceBook.get(side).add(user);
                    }
                    if (!usersAndSides.containsKey(user)) {
                        usersAndSides.put(user, side);
                        forceBook.get(side).add(user);
                    } else {
                        String holder = usersAndSides.get(user);
                        usersAndSides.replace(user, side);
                        forceBook.get(side).add(user);
                        forceBook.get(holder).remove(user);
                    }
                    System.out.printf("%s joins the %s side!\n", user, side);
                }

            }
        }
        printingMap(forceBook);
    }
}