-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1662(Stack).java
More file actions
55 lines (46 loc) · 1.59 KB
/
1662(Stack).java
File metadata and controls
55 lines (46 loc) · 1.59 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int answer = 0;
Stack<String>stack = new Stack<>();
String s = br.readLine();
for(int i = s.length()-1; i >= 0; i--){
String temp = String.valueOf(s.charAt(i));
if(temp.equals(")")){
stack.add(temp);
}else if(temp.equals("(")){
//StringBuilder sb = new StringBuilder();
//StringBuilder add = new StringBuilder();
int leng = 0;
while (!stack.peek().equals(")")){
String pop = stack.pop();
if(pop.equals("---")){
leng += (Integer.parseInt(stack.pop()));
}else{
leng++;
}
}
String num = String.valueOf(s.charAt(i-1));
stack.pop();
int sum = Integer.parseInt(num) * leng;
stack.push(Integer.toString(sum));
stack.push("---");
i--;
}else{
stack.push(temp);
}
}
while (!stack.isEmpty()){
String temp = stack.pop();
if(temp.equals("---")){
temp = stack.pop();
answer += Integer.parseInt(temp);
}else {
answer += temp.length();
}
}
System.out.println(answer);
}
}