[백준] PS/Java [실랜디]

[백준 1384] 메세지 - JAVA

SH3542 2025. 2. 18. 19:17

https://www.acmicpc.net/problem/1384

 

구현 문제

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

class Main {

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int N;
    int idx = 1;
    while ((N = Integer.parseInt(br.readLine())) != 0) {

      StringTokenizer st;
      List<int[]> l = new ArrayList<>();
      Map<Integer, String> m = new HashMap<>();

      for (int i = 0; i < N; i++) {
        st = new StringTokenizer(br.readLine());
        m.put(i, st.nextToken());

        for (int j = 0; j < N - 1; j++) {
          if (st.nextToken().charAt(0) == 'N') {

            int v = i - (j + 1); // 현재 친구 i보다 j+1번째 전의 친구가 나쁜 말을 했다.
            int prev = v < 0 ? N + v : v;

            l.add(new int[]{prev, i});
          }
        }
      }

      System.out.println("Group " + idx++);
      if (l.isEmpty()) {
        System.out.println("Nobody was nasty");
      } else {
        l.forEach(arr -> System.out.println(m.get(arr[0]) + " was nasty about " + m.get(arr[1])));
      }
      System.out.println();
    }


  }
}