๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1406 - ์—๋””ํ„ฐ

by YWTechIT 2021. 5. 10.
728x90

๐Ÿ“Œ ๋ฐฑ์ค€ 1406 - ์—๋””ํ„ฐ

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

์–ด์ œ ์‘์‹œํ–ˆ๋˜ 2021 ์นด์นด์˜ค ์ธํ„ด์‹ญ ์˜จ๋ผ์ธ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ์—์„œ๋„ ๋น„์Šทํ•œ ๋ฌธ์ œ๊ฐ€ ๋‚˜์™”๋‹ค. ๋‹ค๋งŒ ๊ฑฐ๊ธฐ์„œ๋Š” ctl + z๊ธฐ๋Šฅ๋„ ํฌํ•จ๋˜์–ด์žˆ์–ด์„œ ์กฐ๊ธˆ ๋” ์–ด๋ ค์› ๋˜๊ฒƒ ๊ฐ™๋‹ค. ๋‚˜์ค‘์— ํ•ด์„ค์ด ๋‚˜์˜ค๋ฉด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”์ง€ ์‚ดํŽด๋ด์•ผ๊ฒ ๋‹ค.

 

์ด ๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€ ์ •๋ง ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ,  ๊ฒฐ๊ณผ์ ์œผ๋กœ stack ํ˜น์€ deque๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ œ์—์„œ ์‹œ๊ฐ„ ์ œํ•œ์ด 0.3์ดˆ๋กœ ์ฃผ์–ด์ ธ์žˆ์–ด์„œ O(N^2)์œผ๋กœ๋Š” ํ’€ ์ˆ˜ ์—†๋‹ค.

 

์ฒซ ์‹œ๋„์—์„œ len(s)๋งŒํผ cursor๋ฅผ ์„ ์–ธํ•ด์„œ ํ’€์—ˆ์ง€๋งŒ B ๋ช…๋ น์–ด์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด์ค˜์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  cursor๋ฅผ ์‚ฌ์šฉํ•ด์„œ insert์™€ remove๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๊ฐ๊ฐ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N)์ด๊ณ  ์ž…๋ ฅ์ธ ๋ช…๋ น์–ด๋ฅผ ๋ฐ›๊ธฐ์œ„ํ•ด ์ฒ˜์Œ์— ๋ฐ˜๋ณต๋ฌธ์„ ํ•œ๋ฒˆ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์ด ๋˜๋Š” ํ˜•ํƒœ์˜€๋‹ค.

 

์ฒ˜์Œ์— deque๋กœ ํ’€๊ณ , ๋‘๋ฒˆ์งธ๋Š” stack์œผ๋กœ ํ’€์—ˆ๋‹ค. ๋‘ ๋ฐฉ์‹ ๋ชจ๋‘ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(N)์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰์‹œ๊ฐ„์€ ๊ฐ๊ฐ 364ms, 352ms๋กœ ์ฐจ์ด๊ฐ€ ๋งŽ์ง€ ์•Š์•˜๋‹ค.

 

  1. ๋นˆ deque ํ˜น์€ stack์„ ์„ ์–ธํ•œ๋‹ค.
  2. L: ์ปค์„œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ์˜ฎ๊ธฐ๋Š” ๋ช…๋ น์–ด์ธ L์ด ๋“ค์–ด์˜ค๋ฉด L๋ณด๋‹ค ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๊ฐ’๋“ค ๋ชจ๋‘ deque_R๋กœ ์˜ฎ๊ธด๋‹ค. ์˜ฎ๊ธธ๋•Œ๋Š” append๊ฐ€ ์•„๋‹Œ appendleft()๋กœ ํ•ด์ฃผ๋Š”๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž.
  3. D: ๋ฐ˜๋Œ€๋กœ ์ปค์„œ๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์˜ฎ๊ธฐ๋Š” ๋ช…๋ น์–ด์ธ D๊ฐ€ ๋“ค์–ด์˜ค๋ฉด D๋ณด๋‹ค ์™ผ์ชฝ์— ์žˆ๋Š” ๊ฐ’๋“ค ๋ชจ๋‘ deque_L๋กœ ์˜ฎ๊ธด๋‹ค. ์˜ฎ๊ธธ ๋•Œ๋Š” append๊ฐ€ ์•„๋‹Œ appendleft()๋กœ ํ•ด์ฃผ๋Š”๊ฒƒ์„ ์žŠ์ง€ ๋ง์ž.
  4. B: ์‚ญ์ œํ•˜๋Š” ๊ธฐ๋Šฅ์€ pop์œผ๋กœ ํ•ด๊ฒฐํ•œ๋‹ค.
  5. P: ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์€ append๋กœ ํ•ด๊ฒฐํ•œ๋‹ค.

 

๋ช…๋ น์–ด L, D, B ๊ณตํ†ต์ ์œผ๋กœ ์ปค์„œ๊ฐ€ ๋ฌธ์žฅ์˜ ๋งจ ์•ž / ๋งจ ๋’ค๋ฉด ๋ฌด์‹œํ•˜๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๋Š”๋ฐ, ์ด๋Š” deque์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฌด์‹œํ•˜๋Š” ์กฐ๊ฑด์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
deque์˜ ์ถœ๋ ฅ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„œ๋กœ ๋”ํ•ด์ฃผ๊ณ  join์„ ํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ, stack๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์–ด์ค˜์•ผ(reversed) ์ •์ƒ์ ์ธ ์ถœ๋ ฅ์ด ๋‚˜์˜จ๋‹ค.

# deque
import sys
from collections import deque
input = sys.stdin.readline

deque_L, deque_R = list(input().strip()), deque([])
m = int(input())

for i in range(m):
    command = input().split()
    if 'L' in command:
        if not deque_L:
            continue
        deque_R.appendleft(deque_L.pop())

    elif 'D' in command:
        if not deque_R:
            continue
        deque_L.append(deque_R.popleft())

    elif 'B' in command:
        if not deque_L:
            continue
        deque_L.pop()

    else:
        deque_L.append(command[1])

print(''.join(deque_L + list(deque_R)))
# stack
import sys
input = sys.stdin.readline

stack_L, stack_R = list(input().strip()), []
m = int(input())

for i in range(m):
    command = input().split()
    if 'L' in command:
        if not stack_L:
            continue
        stack_R.append(stack_L.pop())

    elif 'D' in command:
        if not stack_R:
            continue
        stack_L.append(stack_R.pop())

    elif 'B' in command:
        if not stack_L:
            continue
        stack_L.pop()

    else:
        stack_L.append(command[1])

print(''.join(stack_L + list(reversed(stack_R))))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€