취미처럼

[백준] 11723번 집합 본문

Algorithm/백준

[백준] 11723번 집합

sirius 2021. 3. 4. 17:26
https://www.acmicpc.net/problem/11723
 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net

 

검색해보니 원래는 비트 연산으로 푸는 문제라고 한다.

 

import java.util.*;
import java.io.*;

public class Main {

    public static LinkedHashSet<Integer> set = new LinkedHashSet<>();
    public static StringBuffer sb = new StringBuffer();
    
    public static void main(String[] args) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        for(int i = 0; i < n ; i++) {
            st = new StringTokenizer(br.readLine());
            String cal = st.nextToken().toString();
            int num = 0;
            if(!"all".equals(cal) && !"empty".equals(cal)) {
           		num = Integer.parseInt(st.nextToken());
            }
            cal(cal, num);
        }
        System.out.println(sb);
    }

    public static void cal(String cal, int num) {
        switch(cal){
            case "add":
            	set.add(num);
            break;
            case "remove":
            	set.remove(num);
            break;
            case "check":
                if(set.contains(num)) {
                	sb.append(1).append("\n");
                } else {
                	sb.append(0).append("\n");
                }
            break;
            case "toggle":
                if(set.contains(num)) {
                	set.remove(num);
                } else {
                	set.add(num);
                }
            break;
            case "all":
                for(int i = 1; i<=20; i++){
                    set.add(i);
                }
            break;
            case "empty":
            	set.clear();
            break;
            default:
            break;

        }

    }
}

 

 

 

'Algorithm > 백준' 카테고리의 다른 글

[백준] 14391번 종이조각  (0) 2021.03.04
[백준] 1182번 부분수열의 합  (0) 2021.03.04
[백준] 6603번 로또  (0) 2021.03.04
[백준] 10971번 외판원 순회 2  (0) 2021.03.04
[백준] 10819번 차이를 최대로  (0) 2021.03.04
Comments