[ ๋ฌธ์ ]
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
'๐ถ Programming > ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ํ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ. 7662 ์ด์ค ์ฐ์ ์์ ํ (Python) (4) | 2022.12.08 |
---|---|
BOJ. 9095 1, 2, 3 ๋ํ๊ธฐ (Python) (2) | 2022.12.05 |
BOJ. 12100 2048 (Easy) (Python) (3) | 2022.10.07 |
BOJ. 1725 ํ์คํ ๊ทธ๋จ (Python) (0) | 2022.10.03 |
2021 ์นด์นด์ค ์ฑ์ฉ์ฐ๊ณํ ์ธํด์ญ Lv2. ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ (0) | 2022.09.11 |