BOJ11726

김현민·2021년 2월 5일
0

Algorithm

목록 보기
22/126
post-thumbnail

BOJ11726 집합

문제

코드

#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    cin.tie(NULL);
    cout.tie(NULL);
    ios::sync_with_stdio(false);
    int n, num;
    string str;
    int bit = 0;

    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> str;

        if (str == "add")
        {
            cin >> num;
            bit |= (1 << num);
        }
        else if (str == "check")
        {
            cin >> num;
            if (bit & (1 << num))
            {
                cout << 1 << '\n';
            }
            else
                cout << 0 << '\n';
        }
        else if (str == "remove")
        {
            cin >> num;
            bit &= ~(1 << num);
        }
        else if (str == "toggle")
        {
            cin >> num;
            bit ^= (1 << num);
        }
        else if (str == "all")
        {
            bit = (1 << 21) - 1;
        }
        else if (str == "empty")
        {
            bit = 0;
        }
    }

    return 0;
}

비트마스킹 정리

| : OR
& : AND
^ : XOR
~ : 반전(NOT)
<< : Shift (왼쪽으로 이동)
>> : Shift (오른쪽으로 이동)

정처기 공부할때 봤던건데, 그땐 이게 언제쓰일 수 있을까 몰랐었다. 찾아보니 DP에도 쓰일 수 있다고 한다.
끝이 없구나 😨

profile
Jr. FE Dev

0개의 댓글