-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_2504.java
More file actions
49 lines (42 loc) · 899 Bytes
/
Copy pathB_2504.java
File metadata and controls
49 lines (42 loc) · 899 Bytes
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
import java.util.Scanner;
import java.util.Stack;
public class B_2504 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
int mul=1;
int result=0;
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<str.length();i++) {
switch(str.charAt(i)) {
case '(':
stack.push('(');
mul*=2;
break;
case'[':
stack.push('[');
mul*=3;
break;
case')':
if(stack.isEmpty()||stack.peek()!='(') {
result=0;
break;
}
if(str.charAt(i-1)=='(')
result+=mul;
stack.pop();
mul/=2; break;
case']':
if(stack.isEmpty()||stack.peek()!='[') {
result=0;break;
}
if(str.charAt(i-1)=='[')
result+=mul;
stack.pop();
mul/=3;break;
default:System.out.println(0); return;
}
}
System.out.println(!stack.isEmpty() ? 0:result);
}
}