알고리즘

백준_11723_집합

shj718 2022. 1. 16. 15:06

비트 연산만 알면 간단히 해결할 수 있는 문제였다. 주의할 점all()이 32비트 전부를 1로 세팅하는 것이 아니라, 20개 비트만 전부 1로 세팅한다는 점이다.

#include <iostream>
#include <string>

using namespace std;

int M, x, bits;
string s;

void add(int a) {
	bits |= (1 << a);
}

void remove(int a) {
	bits &= ~(1 << a);
}

bool check(int a) {
	return bits & (1 << a);
}

void toggle(int a) {
	bits ^= (1 << a);
}

void all() {
	bits = ((1 << 21) - 1);
}

void empty() {
	bits = 0;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> s;
		if (s == "add") {
			cin >> x;
			add(x);
			continue;
		}
		else if (s == "remove") {
			cin >> x;
			remove(x);
			continue;
		}
		else if (s == "check") {
			cin >> x;
			cout << check(x) << '\n';
			continue;
		}
		else if (s == "toggle") {
			cin >> x;
			toggle(x);
			continue;
		}
		else if (s == "all") {
			all();
			continue;
		}
		else if (s == "empty") {
			empty();
			continue;
		}
	}
	return 0;
}