
1 추가0 추가즉, "HashSet 저장 → O(1) 조회 → 결과 누적" 과정을 수행한다.
이 문제는 카드 소유 여부 확인만 필요하므로 순서와 중복이 중요하지 않다.
HashSet(Hash Table 기반)은:
contains())입력:
5
6 3 2 10 7
4
4 2 7 13
| 단계 | 검증카드 | HashSet 조회 | StringBuilder 누적 |
|---|---|---|---|
| 1 | 4 | 없음 | 0 |
| 2 | 2 | 있음 | 0 1 |
| 3 | 7 | 있음 | 0 1 1 |
| 4 | 13 | 없음 | 0 1 1 0 |
최종 출력: 0 1 1 0
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Set<Integer> cards = new HashSet<>();
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) {
cards.add(Integer.parseInt(st.nextToken()));
}
int m = Integer.parseInt(br.readLine());
StringTokenizer checkSt = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m; i++) {
int card = Integer.parseInt(checkSt.nextToken());
sb.append(cards.contains(card) ? "1" : "0");
if (i != m - 1) sb.append(" ");
}
System.out.println(sb);
}
}
1박
2일 !!!!!!