DevYoon

[๋ฐฑ์ค€] 5397. ํ‚ค๋กœ๊ฑฐ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 5397. ํ‚ค๋กœ๊ฑฐ (Python)

gimewn 2022. 11. 23. 14:22

link ๐Ÿ”— https://www.acmicpc.net/problem/5397

 

5397๋ฒˆ: ํ‚ค๋กœ๊ฑฐ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ๊ฐ•์‚ฐ์ด๊ฐ€ ์ž…๋ ฅํ•œ ์ˆœ์„œ๋Œ€๋กœ ๊ธธ์ด๊ฐ€ L์ธ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ L ≤ 1,000,000) ๊ฐ•์‚ฐ์ด๊ฐ€ ๋ฐฑ์ŠคํŽ˜์ด์Šค๋ฅผ ์ž…

www.acmicpc.net

 

๐Ÿ’ฌ > ์™€ <, -, ๋ฌธ์ž์ผ ๊ฒฝ์šฐ๋ฅผ ๊ฐ๊ฐ ์ƒ๊ฐํ•ด์„œ ํ’€์–ด์ฃผ์—ˆ๋‹ค.

๐Ÿ’ฌ >์˜ ๊ฒฝ์šฐ idx๊ฐ€ ๋์ด ์•„๋‹Œ ๊ณณ์— ์žˆ์„ ๋•Œ๋งŒ ์›€์ง์—ฌ์ค€๋‹ค.

๐Ÿ’ฌ <์˜ ๊ฒฝ์šฐ idx๊ฐ€ ์ฒ˜์Œ์ด ์•„๋‹Œ ๊ณณ์— ์žˆ์„ ๋•Œ๋งŒ ์›€์ง์—ฌ์ค€๋‹ค.

๐Ÿ’ฌ -์˜ ๊ฒฝ์šฐ ๋ฌธ์ž๊ฐ€ ์žˆ๊ณ , idx๊ฐ€ 0์ด ์•„๋‹ ๋•Œ๋งŒ ์‹คํ–‰ํ•œ๋‹ค.

 

from collections import deque

T = int(input())

for _ in range(T):
    sentence = input()
    idx = 0
    result = deque()

    for word in sentence:
        if word == '<':
            if idx > 0:
                idx -= 1
        elif word == '>':
            if idx < len(result):
                idx += 1
        elif word == '-':
            if result and idx > 0:
                del result[idx-1]
                idx -= 1
        else:
            result.insert(idx, word)
            idx += 1

    print("".join(result))