๋ฌธ์
๋น์ด์๋ ๊ณต์งํฉ S๊ฐ ์ฃผ์ด์ก์ ๋, ์๋ ์ฐ์ฐ์ ์ํํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
add x: S์ x๋ฅผ ์ถ๊ฐํ๋ค. (1 โค x โค 20) S์ x๊ฐ ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ์๋ ์ฐ์ฐ์ ๋ฌด์ํ๋ค.remove x: S์์ x๋ฅผ ์ ๊ฑฐํ๋ค. (1 โค x โค 20) S์ x๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ์ฐ์ฐ์ ๋ฌด์ํ๋ค.check x: S์ x๊ฐ ์์ผ๋ฉด 1์, ์์ผ๋ฉด 0์ ์ถ๋ ฅํ๋ค. (1 โค x โค 20)toggle x: S์ x๊ฐ ์์ผ๋ฉด x๋ฅผ ์ ๊ฑฐํ๊ณ , ์์ผ๋ฉด x๋ฅผ ์ถ๊ฐํ๋ค. (1 โค x โค 20)all: S๋ฅผ {1, 2, โฆ, 20} ์ผ๋ก ๋ฐ๊พผ๋ค.empty: S๋ฅผ ๊ณต์งํฉ์ผ๋ก ๋ฐ๊พผ๋ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์ํํด์ผ ํ๋ ์ฐ์ฐ์ ์ M (1 โค M โค 3,000,000)์ด ์ฃผ์ด์ง๋ค.
๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์ ์ํํด์ผ ํ๋ ์ฐ์ฐ์ด ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ค.
์ถ๋ ฅ
checkย ์ฐ์ฐ์ด ์ฃผ์ด์ง๋๋ง๋ค, ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ
ํ์ด
1์ฐจ
import sys ; input = sys.stdin.readline
M = int()
S = set()
def add(x: int) :
if _check(x) : return
S.add(x)
def remove(x: int) :
if not _check(x) : return
S.remove(x)
def check(x: int) :
print(_check(x))
def _check(x: int) :
if x in S : return 1
else : return 0
def toggle(x: int) :
if _check(x) : remove(x)
else : add(x)
def all() :
S = set([i + 1 for i in range(20)])
def empty() :
S = set()
if __name__ == "__main__" :
M = int(input().rstrip())
for _ in range(M) :
command = input()
if len(command.split()) > 1 :
command, x = command.split()
x = int(x)
if 'add' in command : add(x)
elif 'remove' in command : remove(x)
elif 'check' in command : check(x)
elif 'toggle' in command : toggle(x)
elif 'all' in command : all()
elif 'empty' in command : empty()- ์..ํ๋ ธ์๊น..?
2์ฐจ - Solved!
import sys ; input = sys.stdin.readline
class Set :
def __init__(self) :
self.set = set()
def add(self, x: int) :
if self._check(x) : return
self.set.add(x)
def remove(self, x: int) :
if not self._check(x) : return
self.set.remove(x)
def check(self, x: int) :
print(self._check(x))
def _check(self, x: int) :
if x in self.set : return 1
else : return 0
def toggle(self, x: int) :
if self._check(x) : self.remove(x)
else : self.add(x)
def all(self) :
self.set = set([i + 1 for i in range(20)])
def empty(self) :
self.set = set()
if __name__ == "__main__" :
M = int(input().rstrip())
S = Set()
for _ in range(M) :
command = input()
if len(command.split()) > 1 :
command, x = command.split()
x = int(x)
if 'add' in command : S.add(x)
elif 'remove' in command : S.remove(x)
elif 'check' in command : S.check(x)
elif 'toggle' in command : S.toggle(x)
elif 'all' in command : S.all()
elif 'empty' in command : S.empty()- ํด๋์ค ๊ตฌ์กฐ๋ก ๋ณ๊ฒฝํ์ฌ ํด๊ฒฐํ๋ค.