• 문제 링크
2371번: 파일 구별하기
메모리에 N개의 파일이 저장되어 있다. 이 문제에서는 편의상 각각의 파일을 수열과 같이 생각하자. 이와 같은 파일들을 구별하기 위해서는 두 개의 파일을 맨 끝까지 읽어보는 작업을 수행해야
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
static List<List<Integer>> lists = new ArrayList<>();
static Set<Integer> set = new HashSet<>();
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), i;
while (n-- > 0) {
List<Integer> list = new ArrayList<>();
while ((i = read()) != -1) list.add(i);
lists.add(list);
}
int k = 0;
while (!isDistinct(k++)) set.clear();
bw.write(String.valueOf(k));
bw.flush();
}
private static boolean isDistinct(int k) {
for (List<Integer> list : lists) {
if (k < list.size()) {
if (set.contains(list.get(k))) return false;
set.add(list.get(k));
}
}
return true;
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
boolean flag = n == 13;
if (flag) n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return flag ? ~n + 1 : n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 9843 LVM - Data Structure / Java (0) | 2024.01.03 |
---|---|
[백준] 6119 Cow Line - Data Structure / Java (0) | 2024.01.02 |
[백준] 15323 ZigZag - Data Structure / Java (0) | 2023.12.31 |
[백준] 29701 모스 부호 - Data Structure / Java (0) | 2023.12.30 |
[백준] 17173 배수들의 합 - Brute Force / Java (0) | 2023.12.29 |
댓글