๐Ÿถ Programming/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€๊ธฐ

BOJ. 11723 ์ง‘ํ•ฉ (Python)

์ง€ ์› 2022. 12. 9. 13:20

[ ๋ฌธ์ œ ]

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

 

11723๋ฒˆ: ์ง‘ํ•ฉ

์ฒซ์งธ ์ค„์— ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์—ฐ์‚ฐ์˜ ์ˆ˜ M (1 ≤ M ≤ 3,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ์—ฐ์‚ฐ์ด ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

[ ๋‚˜์˜ ํ’€์ด ]

๐Ÿ”ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

๋ฌธ์ œ์—์„œ ์ง‘ํ•ฉ S์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’ x๊ฐ€ (1 ≤ x ≤ 20) ๋กœ ๋ฒ”์œ„๊ฐ€ ์ •ํ•ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์— True/False๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

toggle์—ฐ์‚ฐ์ด ์žˆ์–ด์„œ ๋”์šฑ True/False๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ํŽธํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

์—ฐ์‚ฐ์˜ ์ˆ˜๋Š” M (1 ≤ M ≤ 3,000,000) ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐฏ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ sys.stdin.readline ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

input๊ฐ’์„ ์ด ๋ฐฉ์‹์œผ๋กœ ๋ฐ›์œผ๋‹ˆ๊นŒ ๋’ค์— ์ค„ ๋ณ€ํ™˜ ๊ฐ’์ด ํ•จ๊ป˜ ๋“ค์–ด์™€์„œ .split()์„ ์‚ฌ์šฉํ•ด์„œ ์ œ๊ฑฐํ–ˆ๋‹ค.

 

๐Ÿ”ธ ํ’€์ด ์ฝ”๋“œ

import sys
input = sys.stdin.readline

# ์—ฐ์‚ฐ์˜ ์ˆ˜ M
M = int(input())

# ๊ณต์ง‘ํ•ฉ S
S = [False] * 21

for _ in range(M):
    msg = input().strip()
    if msg == 'all':
        S = [True] * 21
    elif msg =='empty':
        S = [False] * 21
    else:
        msg, n = msg.split()
        n = int(n)
        if msg == 'add':
            if S[n] == False:
                S[n] = True
        if msg == 'remove':
            if S[n] == True:
                S[n] = False
        if msg == 'check':
            if S[n] == True:
                print(1)
            else:
                print(0)
        if msg == 'toggle':
            if S[n] == True:
                S[n] = False
            else:
                S[n] = True

 

[ pair์˜ ํ’€์ด ]

๐Ÿ”ธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•

pair๋Š” ์ •์งํ•˜๊ฒŒ append, remove, in, not in ๋“ฑ๋“ฑ์„ ์‚ฌ์šฉํ–ˆ๋‹ค..!

pair์˜ ํ’€์ด ์ค‘์— input ์—ฐ์‚ฐ์„ ๋ฐ”๋กœ splitํ•˜์ง€ ์•Š๊ณ  list๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ์•ž์˜ ๊ฐ’๋งŒ ํ™•์ธ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

์ด๊ฑฐ ์™„์ „ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿ‘

 

๐Ÿ”ธ ํ’€์ด ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input())
arr = list()
for _ in range(n):
    x = list(input().split())
    a = x[0]

    if a == 'add':
        if int(x[1]) not in arr:
            arr.append(int(x[1]))

    elif a == 'remove':
        if int(x[1]) in arr:
            arr.remove(int(x[1]))

    elif a == 'check':
        if int(x[1]) in arr:
            print(1)
        else:
            print(0)

    elif a == 'toggle':
        if int(x[1]) in arr:
            arr.remove(int(x[1]))
        else:
            arr.append(int(x[1]))

    elif a == 'all':
        arr = [i for i in range(1, 21)]

    elif a == 'empty':
        arr.clear()

 

pair ์˜ github : https://github.com/Ui-Seok