Published:
Updated:


Solution

import sys


def solution(S: str) -> str:
    answer = ''
    is_tag = False

    stack = []
    for s in S:
        if s == ' ':
            while stack:
                answer += stack.pop()

            answer += s
            continue

        if s == '<':
            while stack:
                answer += stack.pop()

            is_tag = True
            answer += s
            continue

        if s == '>':
            is_tag = False
            answer += s
            continue

        if is_tag:
            answer += s
            continue

        stack.append(s)

    while stack:
        answer += stack.pop()

    return answer


def main() -> None:
    S = sys.stdin.readline().rstrip()
    print(solution(S))


if __name__ == '__main__':
    main()

Leave a comment