본문 바로가기

PS

[BOJ]11723번 : 집합

https://www.acmicpc.net/problem/11723

 

11723번: 집합

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

www.acmicpc.net

 

분류 : 없음

더보기
import java.io.*;
import java.math.*;
import java.util.*;

class Myscanner {
    BufferedReader br;
    StringTokenizer st;

    public Myscanner() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}

public class Main {
    public static Myscanner sc = new Myscanner();
    public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));

    public static void main(String[] args) throws IOException {
        int N = sc.nextInt();
        List<String> L = new LinkedList<String>();
        List<String> S = new LinkedList<String>();
        for(int i=1;i<=20;i++)
        {
            S.add(i+"");
        }
        for(int z =0; z<N;z++){
            String str = sc.next();
            String num="";
            if(!str.equals("all") && !str.equals("empty")){
            num = sc.next();
            }
            if (str.equals("add"))
            {
                    if(!L.contains(num)){
                        L.add(num);
                    }
            }
            else if (str.equals("remove"))
            {
                if(L.contains(num)){
                    L.remove(num);
                }
            }
            else if (str.equals("check"))
            {
                    if(L.contains(num)){
                        out.println(1);
                    }
                    else{
                        out.println(0);
                    }
            }
            else if (str.equals("toggle"))
            {
                    if(L.contains(num)){
                        L.remove(num);
                    }
                    else{
                        L.add(num);
                    }
            }
            else if (str.equals("all"))
            {
                L.clear();
                L.addAll(S);
            }
            else if (str.equals("empty"))
            {
                L.clear();
            }
        }
        out.flush();
    }
}
 

 

코드가 난잡 하긴 하지만 시간 초과만 조심한다면 로직 자체는 쉬운 문제

'PS' 카테고리의 다른 글

[BOJ1005번 : ACM Craft  (0) 2017.04.04
[BOJ]10422번 : 괄호  (0) 2017.04.03
[BOJ]2206번 : 벽 부수고 이동하기  (0) 2017.04.02
[BOJ]3055번: 탈출  (0) 2017.04.02
[BOJ]1194번 : 달이 차오른다, 가자.  (0) 2017.03.31