File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ /*
5
+ * 괄호의 값
6
+ */
7
+
8
+ public class DH_2504 {
9
+ public static void main (String [] args ) throws Exception {
10
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11
+ String s = br .readLine ();
12
+
13
+ long result = 0 ;
14
+
15
+ Stack <Character > stack = new Stack <Character >(); // 괄호, 숫자 존재에 대한 정보 저장
16
+ Stack <Long > numStack = new Stack <Long >(); // 숫자에 대한 정보 저장
17
+
18
+ for (int i = 0 ; i < s .length (); i ++) {
19
+ char ch = s .charAt (i );
20
+
21
+ if (ch == '(' || ch == '[' ) stack .push (ch ); // 여는 괄호라면 스택에 넣어주기
22
+ else {
23
+
24
+ boolean flag = false ; // 괄호가 잘 닫혔을 때, flag를 true로 만들어주기
25
+
26
+ ch = ch == ')' ? '(' : '[' ; // 괄호 뒤집어주기
27
+
28
+ if (!stack .isEmpty ()) {
29
+
30
+ long tmp = 0 ; // 숫자 계산을 위한 변수
31
+
32
+ while (!stack .isEmpty ()) {
33
+ // 괄호가 같을 때
34
+ if (ch == stack .peek ()) {
35
+ stack .pop ();
36
+ stack .push ('.' ); // 숫자가 있음을 표시
37
+
38
+ int mul = ch == '(' ? 2 : 3 ;
39
+ numStack .push (tmp == 0 ? mul : mul * tmp );
40
+
41
+ // 괄호가 닫혀야 flag true로 해줌
42
+ flag = true ;
43
+ break ;
44
+ }
45
+
46
+ // 숫자가 존재할 때
47
+ else if (stack .peek () == '.' ) {
48
+ stack .pop ();
49
+ tmp += numStack .pop ();
50
+ }
51
+ // 유효하지 않은 입력일 때
52
+ else break ;
53
+ }
54
+ }
55
+
56
+ if (!flag ) break ;
57
+ }
58
+
59
+ }
60
+
61
+ boolean flag = true ;
62
+
63
+ // stack에 괄호가 있는지 확인하면서 최종값 계산해주기
64
+ while (!stack .isEmpty ()) {
65
+ if (stack .pop () == '.' ) result += numStack .pop ();
66
+ else {
67
+ flag = false ;
68
+ break ;
69
+ }
70
+ }
71
+
72
+ if (!flag ) System .out .println (0 );
73
+ else System .out .println (result );
74
+ }
75
+ }
You can’t perform that action at this time.
0 commit comments