Describe:
Now, with a sequence of parentheses, you should check that the line brackets are paired.
-
-
Input:
-
-
the first line enters a number n (0<n<=100), which indicates that there are n sets of test data. The next n lines enter multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data groups is less than 5 groups. Data guarantee S contains only "[", "]", "(", ")" Four characters
-
-
Output:
-
The
-
output of each set of input data is one row, if the parentheses contained in the string are paired, the output is yes, and if you do not pair the output no
-
-
Sample input:
-
-
3[(]) (]) ([[[] ()])
-
-
Sample output:
-
Nonoyes
ImportJava.util.Scanner; Public classNo2 {Privatestring Checkout (String str) {Try{stringbuffer ASB=NewStringBuffer (str);//Analog Stack aStringBuffer BSB =NewStringBuffer ();//Analog Stack B intLength =asb.length (); while(Length > 0) { //In the closing parenthesis, move a into B if(Asb.charat (length-1) = = ') ' | | Asb.charat (length-1) = = '] ') {bsb.append (Asb.charat (length-1)); Asb.deletecharat (Length-1); Length=asb.length (); Continue; } //If the left parenthesis cannot be legally matched with the top of the stack in B, the sequence is illegal; if the legal match, move a shift b if(Asb.charat (length-1) = = ' (') { if(Bsb.charat (Bsb.length ()-1)! = ') ') { return"No"; } Else{Asb.deletecharat (asb.length ()-1); Bsb.deletecharat (Bsb.length ()-1); Length=asb.length (); Continue; } } if(Asb.charat (length-1) = = ' [') { if(Bsb.charat (Bsb.length ()-1)! = '] ') { return"No"; } Else{Asb.deletecharat (asb.length ()-1); Bsb.deletecharat (Bsb.length ()-1); Length=asb.length (); Continue; } } } if(bsb.length () = = 0) { return"Yes"; } } Catch(RuntimeException e) {}return"No"; } Public Static voidMain (String args[])throwsException {Try{Scanner cin=NewScanner (system.in); intnum =Cin.nextint (); String[] Result=NewString[num]; No2 NO2=NewNo2 (); for(inti = 0; i < num; i++) {String str=Cin.next (); Result[i]=no2.checkout (str); } for(inti = 0; i < num; i++) {System.out.println (result[i]); } } Catch(RuntimeException e) {} }}
Brace pairing problem