Logo
(追記) (追記ここまで)

26450번 - Parse the Syntax Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB54373375.000%

문제

To evaluate a program efficiently, a language processor often transforms it into a syntax tree. In this problem you are given a syntax tree of a mathematical expression using ASCII characters. Please evaluate the expression

The syntax tree we consider in this problem is a rooted binary tree where each node has either zero or two children. If a node has zero children, it is an integer node that corresponds to a single integer between 0 and 9, inclusive. On the other hand, if a node has two children, the node is a binary operation node that corresponds to a binary operation of either addition, subtraction or multiplication. In this case the left and right children correspond to the left and right operands of the binary operation, respectively. For example, Figure B.1 represents the syntax tree of expression $(9-4) * ((7*2)+5)$.

Figure B.1: Example of a syntax tree

To represent such a syntax tree using ASCII characters, you are given $H$ strings of $W$ characters. Each character is either ‘+', ‘-', ‘*', a digit between ‘0' and ‘9', or a period that represents a blank. For example, here is the representation of the syntax tree of Figure B.1.

...*.....
.-.....+.
9.4..*..5
....7.2..

Figure B.2 shows the rules (similar to Backus-Naur Form) of such representation of a syntax tree.

Figure B.2: Rules of the representation of a syntax tree

More precisely, the rules are defined as follows.

  • A “cell" is a rectangular region of characters that corresponds to a single node (i.e., either an integer node or a binary operation node) of a syntax tree.
  • A cell corresponding to an integer node contains only a single digit that is the same integer of the node. The height and width of such a cell are 1.
  • A cell $c$ corresponding to a binary operation node $v$ contains a single operator and two other cells as children. More precisely, let $v_1$ and $v_2$ be the left and right children of the binary operation node, respectively. And let $c_1$ and $c_2$ be the cells that correspond to $v_1$ and $v_2,ドル respectively. The height of $c$ is $\max{(h_1, h_2)} + 1$ where $h_1$ and $h_2$ are the heights of $c_1$ and $c_2,ドル respectively. On the other hand, the width of $c$ is $w_1 + w_2 + 1$ where $w_1$ and $w_2$ are the widths of $c_1$ and $c_2,ドル respectively. The topmost row of $c$ consists of $w_1$ periods followed by an operator followed by $w_2$ periods where the operator is either ‘+', ‘-' or ‘*'. $c_1$ is located from the second to the $(h_1+1)$-st rows (from the top) and the first to the $w_1$-st columns (from the left) of $c$. Similarly, $c_2$ is located from the second to the $(h_2+1)$-st rows (from the top) and the $(w_1+2)$-nd to the $(w_1+w_2+1)$-st columns (from the left) of $c$. Note that although $c_1$ and $c_2$ may have different heights, their top borders are always aligned.
  • It is guaranteed by the above rules that no two cells partially overlap each other. In other words, when two cells overlap, then one of them completely contains the other.
  • Any other characters that are not restricted by the above rules are filled by periods.
  • The entire region of characters is the “root" cell. In other words, the cell corresponding to the root node of the syntax tree has height $H$ and width $W$.

Your task is to calculate the mathematical expression that corresponds to the given syntax tree formatted by the above rules.

입력

The input consists of a single test case of the following format.

$H$ $W$

$s_1$

$\vdots$

$s_H$

The first line contains two integers $H$ and $W$ (1ドル \le H, W \le 37$), which represent the height and width of the representation of the given syntax tree. The following $H$ lines consist of strings of length $W$ where each character is either ‘+', ‘-', ‘*', a digit between ‘0' and ‘9', or a period. It is guaranteed that these strings represent a syntax tree of a mathematical expression in a valid form.

출력

Print the calculation result of the mathematical expression that corresponds to the given input.

제한

예제 입력 1

1 1
5

예제 출력 1

5

예제 입력 2

2 3
.-.
9.2

예제 출력 2

7

예제 입력 3

4 9
...*.....
.-.....+.
9.4..*..5
....7.2..

예제 출력 3

95

힌트

출처

Contest > ICPC Japanese Alumni Group > JAG Practice Contest for ICPC Asia Regional > JAG Practice Contest for ICPC 2021 Asia Yokohama Regional B번

Camp > Petrozavodsk Programming Camp > Winter 2023 > Day 1: JAGain in Petrozavodsk B번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /