-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJAlgo_1662.java
More file actions
40 lines (35 loc) · 1.21 KB
/
Copy pathBJAlgo_1662.java
File metadata and controls
40 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BJAlgo_1662 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] strings = br.readLine().split("");
List<Integer> stack = new ArrayList<>();
for (int i = 0; i < strings.length; i++) {
if (strings[i].equals("(")) {
stack.add(-1);
} else if (strings[i].equals(")")) {
int cnt = 0;
while (true) {
int tmp = stack.remove(stack.size() - 1);
if (tmp == -1) {
break;
}
cnt += tmp;
}
stack.add(stack.remove(stack.size() - 1) * cnt);
} else if (i < strings.length - 1 && strings[i + 1].equals("(")) {
stack.add(Integer.parseInt(strings[i]));
} else {
stack.add(1);
}
}
int answer = 0;
for (int s: stack) {
answer += s;
}
System.out.println(answer);
}
}