๋ฐฑ์ค€

๋ฐฑ์ค€ 1406 ์—๋””ํ„ฐ - ํŒŒ์ด์ฌ

stoneeee 2022. 11. 27. 00:10

 

https://www.acmicpc.net/problem/1406

 

1406๋ฒˆ: ์—๋””ํ„ฐ

์ฒซ์งธ ์ค„์—๋Š” ์ดˆ๊ธฐ์— ํŽธ์ง‘๊ธฐ์— ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ ๊ธธ์ด๊ฐ€ N์ด๊ณ , ์˜์–ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 100,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ž…๋ ฅํ•  ๋ช…๋ น์–ด์˜ ๊ฐœ์ˆ˜

www.acmicpc.net

 

 

 

๊ฐ€์žฅ ์ž˜ ์•Œ๋ ค์ง„ ํ’€์ด๋Š” ์Šคํƒ์„ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

์ปค์„œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์— ์œ„์น˜ํ•œ ์Šคํƒ๊ณผ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•œ ์Šคํƒ ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค.

์˜ค๋ฅธ์ชฝ ์Šคํƒ์€ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด๋‘์—ˆ๋‹ค. pop ํ•˜๋ฉด ์™ผ์ชฝ์— ์žˆ๋Š” ์›์†Œ๊ฐ€ ๋‚˜์˜ค๊ณ , append๋„ ์™ผ์ชฝ์— ๋œ๋‹ค.

 

์ฒ˜์Œ์—๋Š” ๋ฌธ์žฅ์˜ ๋งจ ๋’ค์— ์ปค์„œ๊ฐ€ ์œ„์น˜ํ•˜๋ฏ€๋กœ ์™ผ์ชฝ ์Šคํƒ์— ๋ฌธ์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์–ด๋‘๋ฉด ๋œ๋‹ค. ์ปค์„œ๋Š” ํ•ญ์ƒ left ๋ฆฌ์ŠคํŠธ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋‹ค.

 

 

๋งŒ์•ฝ "P"๋ผ๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด "$" ์›์†Œ๋ฅผ ์ปค์„œ ์™ผ์ชฝ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ƒฅ ์ปค์„œ ์™ผ์ชฝ์ธ left ๋ฆฌ์ŠคํŠธ์— append ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

"L"์ด๋ผ๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ปค์„œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ์˜ฎ๊ฒจ์•ผ ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ปค์„œ๋ฅผ ์ง์ ‘ ์˜ฎ๊ธธ ํ•„์š”๋Š” ์—†๋‹ค.

๊ทธ๋ƒฅ left ๋ฆฌ์ŠคํŠธ์—์„œ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ pop ํ•ด์„œ right ๋ฆฌ์ŠคํŠธ์— append ํ•ด์ฃผ๋ฉด ์ปค์„œ๋ฅผ ์˜ฎ๊ธด ๊ฒƒ๊ณผ ๊ฐ™์€ ํšจ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

"L" ๋ช…๋ น์–ด๋ฅผ ๋‘ ๋ฒˆ ์—ฐ์† ์ˆ˜ํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. right ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋’ค์ง‘ํ˜€ ์žˆ๋‹ค๋Š” ๊ฑธ ์žŠ์ง€ ๋ง์ž.

 

 

๋ฐ˜๋Œ€๋กœ "D" ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด right ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ pop ํ•ด์„œ left ๋ฆฌ์ŠคํŠธ์— append ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 

 

"B" ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ปค์„œ ์™ผ์ชฝ์˜ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ƒฅ left ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜ pop ํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

๋ชจ๋“  ์กฐ์ž‘์ด ๋๋‚˜๋ฉด ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. 

์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ€ "ABDX"๊ฐ€ ๋œ๋‹ค.

right ๋ฆฌ์ŠคํŠธ๋Š” ๋’ค์ง‘ํ˜€์žˆ๋Š” ๊ฑธ ๊ณ ๋ คํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•  ๋•Œ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ pop ํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋‹ˆ๊นŒ left๋‚˜ right์— ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ๊ผญ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์•ˆํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋œฐ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

from sys import stdin

left = list(input())
right = []

for _ in range(int(input())):
    command = list(stdin.readline().split())
    if command[0] == 'L' and left:
        right.append(left.pop())
    elif command[0] == 'D' and right:
        left.append(right.pop())
    elif command[0] == 'B' and left:
        left.pop()
    elif command[0] == 'P':
        left.append(command[1])

answer = left + right[::-1]
print(''.join(answer))

 

 


 

 

์‚ฌ์‹ค ์ด ๋ฌธ์ œ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

๊ณต๋ถ€๋ฅผ ํ•ด๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณผ ์ˆ˜๋Š” ์žˆ๋‹ค.

์ฐธ๊ณ ๋กœ ๋‚ด๊ฐ€ ์ง  ๋”๋Ÿฌ์šด ์ฝ”๋“œ๋Š” ์Šคํƒ ํ’€์ด๋ณด๋‹ค ์„ฑ๋Šฅ ๋ฉด์—์„œ 4๋ฐฐ ์ •๋„ ๋Š๋ ธ๋‹ค.

 

 

from sys import stdin

# ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ ํด๋ž˜์Šค
class ListNode:
    def __init__(self, val, prev, next):
        self.val = val
        self.prev = prev
        self.next = next

# ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ head
head = ListNode('dummy', None, None)
curr = head

init = input() # ์ดˆ๊ธฐ ๋ฌธ์ž์—ด
for i in range(len(init)): # ์ดˆ๊ธฐ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์–ด ๋‘ 
    new_node = ListNode(init[i], None, None)
    curr.next = new_node
    new_node.prev = curr
    curr = new_node

# ๋ช…๋ น์–ด๋“ค ์ฒ˜๋ฆฌ
for _ in range(int(input())):
    command = stdin.readline().rstrip()
    if command == 'L':
        if curr.val != 'dummy':
            curr = curr.prev
    elif command == 'D':
        if curr.next:
            curr = curr.next
    elif command == 'B':
        if curr.val != 'dummy':
            curr = curr.prev
            if curr.next.next:
                curr.next = curr.next.next
                curr.next.prev = curr
            else:
                curr.next = None
    else:
        new_node = ListNode(command[-1], None, None)
        if curr.next:
            new_node.next = curr.next
            curr.next.prev = new_node
        curr.next = new_node
        new_node.prev = curr
        curr = new_node

# head ๋‹ค์Œ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ถœ๋ ฅ
print_node = head.next
while print_node:
    print(print_node.val, end='')
    print_node = print_node.next