์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) 10828 ํŒŒ์ด์ฌ - ์Šคํƒ

  • -

๋ฌธ์ œ ๋งํฌ : https://www.acmicpc.net/problem/10828

 

10828๋ฒˆ: ์Šคํƒ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€ ์•Š์€ ๋ช…๋ น์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

www.acmicpc.net

 

 

๋”ฑํžˆ ์–ด๋ ค์› ๋˜๊ฑด ์—†์—ˆ์ง€๋งŒ sys.readline.rstrip() ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ input()์œผ๋กœ ๋ฐ›์•˜๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

import sys
input = sys.stdin.readline
N = int(input())
stack = []
for i in range(N):

    command = input().rstrip()
    
    if " " in command :    #์ž…๋ ฅ ๋ฌธ์ž์—ด์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ๊ฑด push๋ฐ–์— ์—†๋‹ค. 
        c,n = command.split()
        stack.append(int(n))
    elif command == 'pop' : 
        if len(stack) > 0 : print(stack.pop(-1))
        else : print(-1)

    elif command == 'size' : print(len(stack))
    elif command == 'empty' : print(1 if len(stack)==0 else 0)
    elif command == 'top' :
        if len(stack) > 0 : print(stack[-1])
        else : print(-1)

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.