티스토리 뷰

PS/boj

boj)11723 - 집합

kingsubin 2020. 12. 9. 20:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.*;
import java.util.StringTokenizer;
 
public class boj_11723 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringTokenizer st;
    static int s = 0;
 
    public static void main(String[] args) throws IOException {
        int m = Integer.parseInt(br.readLine());
        for (int i = 0; i < m; i++) {
            st = new StringTokenizer(br.readLine());
            String op = st.nextToken();
            if (op.equals("all")) {
                s = (1 << 20- 1;
            } else if (op.equals("empty")) {
                s = 0;
            } else {
                int val = Integer.parseInt(st.nextToken());
                operate(op, val);
            }
        }
        bw.flush();
        bw.close();
    }
 
    static void operate(String op, int val) throws IOException {
        switch (op) {
            case "add" -> {
                if ((s & (1 << val)) == 0) {
                    s |= (1 << val);
                }
            }
            case "remove" -> {
                if ((s & (1 << val)) != 0) {
                    s &= ~(1 << val);
                }
            }
            case "toggle" -> s ^= (1 << val);
            default -> {
                if ((s & (1 << val)) == 0) {
                    bw.append("0");
                } else {
                    bw.append("1");
                }
                bw.newLine();
            }
        }
    }
}
 
cs

 

- 비트마스크

 

- switch문 바뀐거로 해서 java 15로 돌려봤는데 메모리 초과 떴는데 더 이상 어떻게 고쳐야할지 모르겠음

- 맞는거같은데 ,, 뭐가 문제지 ..

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
import java.io.*;
public class boj_11723 {
    public static void main(String args[]) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = 20;
        int m = Integer.valueOf(bf.readLine());
        int s = 0;
        StringBuilder sb = new StringBuilder();
        while (m-- > 0) {
            String[] line = bf.readLine().split(" ");
            if (line[0].equals("add")) {
                int x = Integer.valueOf(line[1]) - 1
                s = (s | (1 << x));
            } else if (line[0].equals("remove")) {
                int x = Integer.valueOf(line[1]) - 1
                s = (s & ~(1<<x));
            } else if (line[0].equals("check")) {
                int x = Integer.valueOf(line[1]) - 1
                int res = (s & (1 << x));
                if (res == 0) {
                    sb.append("0\n");
                } else {
                    sb.append("1\n");
                }
            } else if (line[0].equals("toggle")) {
                int x = Integer.valueOf(line[1]) - 1
                s = (s ^ (1 << x));
            } else if (line[0].equals("all")) {
                s = (1 << n) - 1;
            } else if (line[0].equals("empty")) {
                s = 0;
            }
        }
        System.out.print(sb);
    }
}
cs

 

- 똑같은 코드로 JAVA 15 돌리면 메모리 초과나온다.

- JAVA 8, JAVA 11 에서는 통과

- JAVA 15 에 문제가 있는듯 ,,

 

 

 

 


www.acmicpc.net/problem/11723

'PS > boj' 카테고리의 다른 글

boj)1991 - 트리 순회  (0) 2020.12.11
boj)7562 - 나이트의 이동  (0) 2020.12.10
boj)14391 - 종이 조각  (0) 2020.12.09
boj)2529 - 부등호  (0) 2020.12.08
boj)15661 - 링크와 스타트  (0) 2020.12.06