알고리즘 풀이/백준

[백준][Java] 11723번 집합 (비트마스크) #2

배게 2021. 10. 5. 16:41
728x90

바로 다시 풀어 봤는데 바로 안풀림

1. x-1을 x로 실수

2. toggle 부분 실수

Line:44 

bitset ^= ~(1<<(x-1));

-> bitset ^= 1<<(x-1);

 

 

 

 

 

참조 : https://www.acmicpc.net/source/33706867

 

for문 20번 돌리는 것 할 필요가 없었음

비트 연산에 대한 이해가 조금 부족해서 다른 문제를 더 풀어봐야겠음

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
53
54
55
56
57
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
public class Main {
    
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
    
    public static void main(String[] args) throws IOException{
        int M = stoi(br.readLine());
        int bitset = 0;
        
        for(int i=0; i<M; i++) {
            String[] str = br.readLine().split(" ");
            String command = str[0];
            
            if (command.equals("all")) {
                bitset |= ~(0);
                continue;
            }
            else if (command.equals("empty")) {
                bitset = 0;
                continue;
            }
            
            int x = stoi(str[1]);
            
            if(command.equals("add")) {
                bitset |= (1<<(x-1));
            }
            else if (command.equals("check")) {
                int check = ((bitset&(1<<(x-1)))!=0)? 1:0
                bw.write( check+"\n");
            } 
            else if (command.equals("remove")) {
                bitset &= ~(1<<(x-1));
            } 
            else if (command.equals("toggle")) {
                bitset ^= (1<<(x-1));
            } 
        }
        
        
        bw.flush();
        bw.close();
    }
    
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs

 

 

 

 

(더럽게 푼 것)

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
public class Main {
    
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 
    
    public static void main(String[] args) throws IOException{
        int M = stoi(br.readLine());
        
        int arr = 0;
        
        for(int i=0; i<M; i++) {
            String[] str = br.readLine().split(" ");
            String command = str[0];
            
            if (command.equals("all")) {
                arr = 0;
                for(int j=0; j<20; j++)
                    arr = (arr|(1<<j));
                continue;
            }
            else if (command.equals("empty")) {
                arr = 0;
                continue;
            }
            
            int x = stoi(str[1]);
            
            if(command.equals("add")) {
                arr = arr|(1<<(x-1));
            }
            else if (command.equals("check")) {
                int temp = ((arr&(1<<(x-1)))!=0)? 1:0
                bw.write( temp+"\n");
            } 
            else if (command.equals("remove")) {
                int temp = 0;
                for(int j=0; j<20; j++)
                    if(j!=x-1
                        temp = temp|(arr&(1<<j));
                arr = temp;
            } 
            else if (command.equals("toggle")) {
                int temp = 0;
                for(int j=0; j<20; j++) {
                    if(j==x-1) {
                        temp = temp|(arr^(1<<j));
                    }
                    else 
                        temp = temp|(arr&(1<<j));
                }
                arr = temp;
            } 
        }
        
        
        bw.flush();
        bw.close();
    }
    
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs