https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
비트마스킹으로 풀면 실행시간이 조금 더 빠를 겁니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static int M, x;
static String input;
public static void main(String[] args) throws Exception {
HashSet<Integer> set = new HashSet<>();
M = Integer.parseInt(br.readLine());
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine(), " ");
input = st.nextToken();
if ("add".equals(input)) {
set.add(Integer.parseInt(st.nextToken()));
} else if ("remove".equals(input)) {
set.remove(Integer.parseInt(st.nextToken()));
} else if ("check".equals(input)) {
sb.append(set.contains(Integer.parseInt(st.nextToken())) ? 1 : 0).append("\n");
} else if ("toggle".equals(input)) {
x = Integer.parseInt(st.nextToken());
if (set.contains(x)) set.remove(x);
else set.add(x);
} else if ("all".equals(input)) {
for (int j = 1; j <= 20; j++) {
set.add(j);
}
} else set.clear();
}
System.out.print(sb);
}
}
'OJ' 카테고리의 다른 글
[BOJ] 17391 무한부스터 (JAVA) (0) | 2023.04.08 |
---|---|
[BOJ] 4883 삼각 그래프 (JAVA) (0) | 2023.04.07 |
[BOJ] 1051 숫자 정사각형 (JAVA) (0) | 2023.04.05 |
[BOJ] 2126 지진 (JAVA) (0) | 2023.04.04 |
[BOJ] 14574 헤븐스 키친 (JAVA) (0) | 2023.04.03 |