๋ฌธ์ œ

๋น„์–ด์žˆ๋Š” ๊ณต์ง‘ํ•ฉ 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()
  • ํด๋ž˜์Šค ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ํ•ด๊ฒฐํ–ˆ๋‹ค.