• 문제 링크
https://www.acmicpc.net/problem/4126
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Main {
static String a, b;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
Map<String, Set<String>> map = new TreeMap<>();
int n = Integer.parseInt(br.readLine());
while (n-- > 0) {
divide(br.readLine());
map.computeIfAbsent(b, k -> new HashSet<>()).add(a);
}
for (String k : map.keySet()) sb.append(k).append(" ").append(map.get(k).size()).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static void divide(String s) {
int i = s.lastIndexOf(" ");
a = s.substring(0, i);
b = s.substring(i + 1);
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 7782 Alien - Brute Force / Java (0) | 2024.11.30 |
---|---|
[백준] 30063 Pabėgimo kambarys - Data Structure / Java (0) | 2024.11.29 |
[백준] 7640 Matryoshka Dolls - Data Structure / Java (0) | 2024.11.27 |
[백준] 24571 Good Groups - Data Structure / Java (0) | 2024.11.26 |
[백준] 4992 Hanafuda Shuffle - Data Structure / Java (0) | 2024.11.25 |
댓글