PS/BOJ
백준 11723번: 집합 (C++)
도비(Doby)
2022. 7. 17. 23:23
https://www.acmicpc.net/problem/11723
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
Solved By: Bitmask
9달 전에 풀었었던 문제입니다. 비트 마스킹을 공부하기 위해 비트 마스킹을 이용하여 문제를 다시 풀어보았습니다.
(9달 전에는 배열을 하나 선언하여 index을 각 번호로 두고 문제를 풀었습니다.)
#include <iostream>
#include <vector>
using namespace std;
int n;
int bit = 0;
vector<int> ans;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
string s;
for(int i = 0; i < n; i++){
cin >> s;
if(s == "all" || s == "empty"){
if(s == "all"){
bit ^= (1 << 21) - 1;
}
else{
bit = 0;
}
}
else{
int w; cin >> w;
if(s == "add"){
bit |= (1 << w);
}
else if(s == "remove"){
bit &= ~(1 << w);
}
else if(s == "check"){
if(bit & (1 << w)) cout << 1 << '\n';
else cout << 0 << '\n';
}
else{
bit ^= (1 << w);
}
}
}
return 0;
}