1
+ /*
2
+ Sanjit Bhat
3
+ ACSL Enclosure
4
+ Contest #2 2017-18
5
+ Acton Boxborough Regional High School
6
+ Senior Division
7
+ */
8
+
9
+ import java .io .*;
10
+ import java .util .*;
11
+
12
+ public class enclosure_sanjit_bhat {
13
+ static String your_name = "Sanjit Bhat" ;
14
+ static String prob_name = "enclosure" ;
15
+
16
+ public static void main (String [] args ) throws IOException {
17
+ Scanner sc = new Scanner (new File (prob_name + ".in" ));
18
+ PrintWriter out = new PrintWriter (new FileWriter (prob_name + ".out" ), true );
19
+ out .println (your_name );
20
+
21
+ // Note: watch out when testing this problem. ACSL incorrectly uses different '-' chars in different test cases
22
+ for (int test_case = 0 ; test_case < 10 ; test_case ++) {
23
+ try {
24
+ // read in input
25
+ String raw_input = sc .nextLine ();
26
+ raw_input = "+" + raw_input + "+" ; // for one indexing and end curly brace case
27
+
28
+ // identify missing enclosure
29
+ char [] operators = {'+' , '−' , '–' , '*' , '/' }; // see the note in line 21
30
+ char [] enclosures = {'{' , '[' , '(' , ')' , ']' , '}' };
31
+ boolean [] enclosures_exist = new boolean [6 ];
32
+ for (int i = 0 ; i < enclosures .length ; i ++) {
33
+ if (raw_input .indexOf (enclosures [i ]) != -1 ) enclosures_exist [i ] = true ;
34
+ }
35
+ int missing = 0 ;
36
+ for (int i = 0 ; i < 6 ; i ++) {
37
+ if (enclosures_exist [i ] && !enclosures_exist [6 -i -1 ]) {
38
+ missing = 6 -i -1 ;
39
+ }
40
+ }
41
+ char conj_missing = enclosures [6 -missing -1 ];
42
+
43
+ // reverse string if missing starting enclosure to simplify for loop stack simulation
44
+ raw_input = (missing < 3 ) ? (new StringBuilder (raw_input )).reverse ().toString () : raw_input ;
45
+
46
+ ArrayList <Integer > output_locs = new ArrayList <>();
47
+ Stack <Character > st_enclosure = new Stack <>(); // this is the KEY data structure for enclosure matching
48
+ boolean only_nums = true ;
49
+
50
+ for (int i = 0 ; i < raw_input .length (); i ++) {
51
+ char curr_char = raw_input .charAt (i );
52
+
53
+ boolean is_enclosure = false ;
54
+ char conj_enclosure = 'x' ;
55
+ for (int j = 0 ; j < enclosures .length ; j ++) {
56
+ if (curr_char == enclosures [j ]) {
57
+ is_enclosure = true ;
58
+ conj_enclosure = enclosures [6 -j -1 ];
59
+ }
60
+ }
61
+
62
+ boolean is_operator = false ;
63
+ for (char c : operators ) {
64
+ if (curr_char == c ) is_operator = true ;
65
+ }
66
+
67
+ // if start enclosure (conjugate not in stack), push first
68
+ if (is_enclosure && st_enclosure .search (conj_enclosure ) == -1 ) st_enclosure .push (curr_char );
69
+
70
+ // checks for 1) more than single int enclosed, 2) enclosures matched, 3) non-number
71
+ if (!only_nums && !st_enclosure .isEmpty () && st_enclosure .peek () == conj_missing && (is_operator || is_enclosure )) output_locs .add (i );
72
+
73
+ // if end enclosure (conjugate in stack), 2 cases
74
+ if (is_enclosure && st_enclosure .search (conj_enclosure ) != -1 ) {
75
+ if (st_enclosure .peek () == conj_enclosure ) {
76
+ st_enclosure .pop ();
77
+ } else st_enclosure .push (curr_char );
78
+ }
79
+
80
+ // 1) conjugate of missing enclosure there and 2) operator there -> we have more than single int enclosed
81
+ if (st_enclosure .search (conj_missing ) != -1 && is_operator ) only_nums = false ;
82
+ }
83
+
84
+ // output answers
85
+ for (int i = 0 ; i < output_locs .size (); i ++) {
86
+ if (missing < 3 ) {
87
+ // deal with reversed string indexing
88
+ out .print (raw_input .length () - output_locs .get (output_locs .size ()-i -1 ));
89
+ } else out .print (output_locs .get (i ));
90
+ if (i != output_locs .size ()-1 ) out .print (", " );
91
+ }
92
+ out .println ();
93
+
94
+ } catch (Exception e ) {
95
+ out .println ("Something's wrong with this test case" );
96
+ e .printStackTrace ();
97
+ }
98
+ }
99
+ sc .close ();
100
+ out .close ();
101
+ }
102
+ }
0 commit comments