File tree Expand file tree Collapse file tree 1 file changed +81
-0
lines changed
Expand file tree Collapse file tree 1 file changed +81
-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+ public static void main (String [] args ) throws Exception {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ String str = br. readLine();
9+
10+ Stack<Character > stack = new Stack<> ();
11+ Stack<Integer > values = new Stack<> ();
12+
13+ for (int i = 0 ; i < str. length(); i++ ) {
14+ char c = str. charAt(i);
15+
16+ if (c == ' (' || c == ' [' ) {
17+ stack. push(c);
18+ values. push(- 1 );
19+ } else {
20+ if (stack. isEmpty()) {
21+ System . out. println(0 );
22+ return ;
23+ }
24+
25+ if (c == ' )' ) {
26+ if (stack. peek() != ' (' ) {
27+ System . out. println(0 );
28+ return ;
29+ }
30+ stack. pop();
31+
32+ int sum = 0 ;
33+ while (! values. isEmpty() && values. peek() != - 1 ) {
34+ sum += values. pop();
35+ }
36+ if (values. isEmpty() || values. peek() != - 1 ) {
37+ System . out. println(0 );
38+ return ;
39+ }
40+ values. pop();
41+ values. push(sum == 0 ? 2 : sum * 2 );
42+ } else if (c == ' ]' ) {
43+ if (stack. peek() != ' [' ) {
44+ System . out. println(0 );
45+ return ;
46+ }
47+ stack. pop();
48+
49+ int sum = 0 ;
50+ while (! values. isEmpty() && values. peek() != - 1 ) {
51+ sum += values. pop();
52+ }
53+ if (values. isEmpty() || values. peek() != - 1 ) {
54+ System . out. println(0 );
55+ return ;
56+ }
57+ values. pop();
58+ values. push(sum == 0 ? 3 : sum * 3 );
59+ }
60+ }
61+ }
62+
63+ if (! stack. isEmpty()) {
64+ System . out. println(0 );
65+ return ;
66+ }
67+
68+ int result = 0 ;
69+ while (! values. isEmpty()) {
70+ if (values. peek() == - 1 ) {
71+ System . out. println(0 );
72+ return ;
73+ }
74+ result += values. pop();
75+ }
76+
77+ System . out. println(result);
78+ }
79+ }
80+
81+ ```
You can’t perform that action at this time.
0 commit comments