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