Skip to content

Commit 08e262c

Browse files
authored
Merge pull request #1108 from AlgorithmWithGod/zinnnn37
[20251013] BOJ / G5 / 괄호의 값 / 김민진
2 parents f6b135f + 0b00e91 commit 08e262c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Stack;
4+
5+
public class BJ_2504_괄호의_값 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
10+
private static int ans, tmp, len;
11+
private static String str;
12+
13+
private static Stack<Character> stack;
14+
15+
public static void main(String[] args) throws IOException {
16+
init();
17+
sol();
18+
}
19+
20+
private static void init() throws IOException {
21+
str = br.readLine();
22+
len = str.length();
23+
ans = 0;
24+
tmp = 1;
25+
stack = new Stack<>();
26+
}
27+
28+
private static void sol() throws IOException {
29+
for (int i = 0; i < len; i++) {
30+
char c = str.charAt(i);
31+
32+
if (c == '(') {
33+
tmp *= 2;
34+
stack.push(c);
35+
} else if (c == '[') {
36+
tmp *= 3;
37+
stack.push(c);
38+
} else {
39+
if (stack.isEmpty() || c == ')' && stack.peek() != '(' || c == ']' && stack.peek() != '[') {
40+
ans = 0;
41+
break;
42+
}
43+
44+
if (c == ')') {
45+
if (str.charAt(i - 1) == '(') {
46+
ans += tmp;
47+
}
48+
tmp /= 2;
49+
stack.pop();
50+
} else if (c == ']') {
51+
if (str.charAt(i - 1) == '[') {
52+
ans += tmp;
53+
}
54+
tmp /= 3;
55+
stack.pop();
56+
}
57+
}
58+
}
59+
60+
if (!stack.isEmpty()) {
61+
ans = 0;
62+
}
63+
64+
bw.write(ans + "");
65+
bw.flush();
66+
bw.close();
67+
br.close();
68+
}
69+
70+
}
71+
```

0 commit comments

Comments
 (0)