[백준] #11723 집합 python

2023. 6. 28. 16:28

https://www.acmicpc.net/problem/11723

 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net

 

 

 

📕 나의 풀이 📕

문제의 각 조건에 맞게 구성하여 풀이하였다.

split을 통해 2개를 받냐 1개를 받냐가 관건이었다.

꽤나 수월하지만 오류가 많이 났던 문제이다.

 

🧑🏻‍💻 나의 코드 🧑🏻‍💻

 

import sys

S = set()
N = int(sys.stdin.readline())

for _ in range(N):
    word_and_x = sys.stdin.readline().strip().split()
    if len(word_and_x) == 1:
        if word_and_x[0] == 'all':
            S = set([i for i in range(1, 21)])
        elif word_and_x[0] == 'empty':
            S = set()
    else:
        word, x = word_and_x[0], word_and_x[1]
        x = int(x)
        if word == 'add':
            S.add(x)
        elif word == 'remove':
            S.discard(x)
        elif word == 'check':
            if x in S:
                print(1)
            else:
                print(0)
        elif word == 'toggle':
            if x in S:
                S.discard(x)
            else:
                S.add(x)

 

BELATED ARTICLES

more