File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+
7+ public static void main (String [] args ) throws Exception {
8+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
9+ String s = br. readLine();
10+
11+ Stack<Character > stack = new Stack<> ();
12+
13+ int temp = 1 ;
14+ int result = 0 ;
15+
16+ for (int i = 0 ; i < s. length(); i++ ) {
17+ char c = s. charAt(i);
18+ if (c == ' (' ) {
19+ temp *= 2 ;
20+ stack. push(c);
21+ } else if (c == ' [' ) {
22+ temp *= 3 ;
23+ stack. push(c);
24+ } else if (c == ' )' ) {
25+ if (stack. isEmpty() || stack. peek() != ' (' ) {
26+ System . out. println(0 );
27+ return ;
28+ }
29+ if (i > 0 && s. charAt(i - 1 ) == ' (' ) {
30+ result += temp;
31+ }
32+ stack. pop();
33+ temp /= 2 ;
34+ } else if (c == ' ]' ) {
35+ if (stack. isEmpty() || stack. peek() != ' [' ) {
36+ System . out. println(0 );
37+ return ;
38+ }
39+ if (i > 0 && s. charAt(i - 1 ) == ' [' ) {
40+ result += temp;
41+ }
42+ stack. pop();
43+ temp /= 3 ;
44+ }
45+ }
46+
47+ if (! stack. isEmpty()) {
48+ System . out. println(0 );
49+ } else {
50+ System . out. println(result);
51+ }
52+ }
53+ }
54+ ```
You can’t perform that action at this time.
0 commit comments