Gardening

시간 제한1초메모리 제한2048 MB

요약
괄호 문자열로 주어진 트리를 파싱한 뒤, 가장 왼쪽 잎부터 차례로 제거하며 그 순서를 출력한다.
난이도

보통10점 중 5점

유형
트리, DFS, 구현, 문자열
정답자
아직 제출이 없습니다

문제

In preparation for the Fashionable Park Competition (FPC), you have set out to prune all the trees of your town's park into fashionable shapes. With so much work to do, you decide that you might as well make a fun game out of it. You take a tree that looks particularly odd, label all the branches, and start chopping off branches according to the following rules:

  • Only a leaf can be chopped off the tree.
  • When choosing between multiple leaves to chop, start with the leftmost one.

The tree can be codified as a string. Each node has a non-unique label on it (a lowercase letter) and a list of children. The children of the node are given as a list of labels between '(' and ')' and separated by ','. Note that a leaf (a node with no children) is not followed by a list of labels.

The tree in Figure G.1 shows a visualisation of the codified tree of the first sample input. For this tree, the leaf with label 'b' should be chopped first, followed by 'd', 'e', and 'f'. Node 'c' is now a leaf, so it should be chopped next. Finally, 'a' can be chopped as well.

Figure G.1: A visualisation of the first sample input.

Given the codification of a tree, parse it and compute the order in which the nodes should be chopped off the tree, if you would chop them off all the way to the root.

입력

The input consists of:

  • A line with a single string ss (1≤∣s∣≤1051\leq |s|\leq 10^5), the tree codification.

The string ss contains no spaces, and you can assume that it is a correct codification for some tree.

출력

A string consisting of the nodes' labels, printed in the order in which they should be chopped.

예제2

  1. 예제 1

    입력
    a(b,c(d,e,f))
    
    예상 출력
    bdefca
    
  2. 예제 2

    입력
    t(z(t(z,t(c,c))),y)
    
    예상 출력
    zccttzyt