-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2504.java
More file actions
52 lines (49 loc) · 1.73 KB
/
2504.java
File metadata and controls
52 lines (49 loc) · 1.73 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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
Stack<Character> stack = new Stack<>();
long sum=0; // 결과
long multiply=1;
for(int i=0; i<input.length(); i++){
char c = input.charAt(i);
if(c=='('){ //여는 괄호는 push 후 값 곱하기
stack.push(c);
multiply*=2;
}
else if(c=='['){
stack.push(c);
multiply*=3;
}
else if(c==')'){ // 닫힌 괄호는 pop
if(stack.isEmpty()||stack.peek()!='('){ // 스택이 비어있거나 괄호쌍이 아니면 0
sum=0;
break;
}
if(input.charAt(i-1)=='('){
sum+=multiply; // 바깥부터 가장 안쪽까지 곱하고 최종 값을 더함
}
stack.pop();
multiply/=2; // 안쪽부터 바깥까지 다시 차례대로 닫힌 괄호가 나오면서 multiply를 되돌리기
}
else if(c==']'){
if(stack.isEmpty()||stack.peek()!='['){ // 스택이 비어있거나 괄호쌍이 아니면 0
sum=0;
break;
}
if(input.charAt(i-1)=='['){
sum+=multiply;
}
stack.pop();
multiply/=3;
}
}
if(!stack.isEmpty()){
System.out.println(0);
}else{
System.out.println(sum);
}
}
}