Skip to content

Commit 6121a7b

Browse files
authored
[20251208] BOJ / G5 / 괄호의 값 / 강신지
1 parent 5b6595a commit 6121a7b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) throws Exception {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
String s = br.readLine();
10+
11+
Stack<Character> stack = new Stack<>();
12+
13+
int temp = 1;
14+
int result = 0;
15+
16+
for (int i = 0; i < s.length(); i++) {
17+
char c = s.charAt(i);
18+
if (c == '(') {
19+
temp *= 2;
20+
stack.push(c);
21+
} else if (c == '[') {
22+
temp *= 3;
23+
stack.push(c);
24+
} else if (c == ')') {
25+
if (stack.isEmpty() || stack.peek() != '(') {
26+
System.out.println(0);
27+
return;
28+
}
29+
if (i > 0 && s.charAt(i - 1) == '(') {
30+
result += temp;
31+
}
32+
stack.pop();
33+
temp /= 2;
34+
} else if (c == ']') {
35+
if (stack.isEmpty() || stack.peek() != '[') {
36+
System.out.println(0);
37+
return;
38+
}
39+
if (i > 0 && s.charAt(i - 1) == '[') {
40+
result += temp;
41+
}
42+
stack.pop();
43+
temp /= 3;
44+
}
45+
}
46+
47+
if (!stack.isEmpty()) {
48+
System.out.println(0);
49+
} else {
50+
System.out.println(result);
51+
}
52+
}
53+
}
54+
```

0 commit comments

Comments
 (0)