Bracket matching problem time limit: 3000 MS | memory limit: 65535 KB difficulty: 3
-
Description
-
Now, there is a sequence of parentheses. Please check whether this line of parentheses is paired.
-
Input
-
Enter N (0 <N <= 100) in the first row, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data. Each group of input data is a string of S (the length of S is less than 10000, and S is not an empty string), and the number of test data groups is less than five. Data guarantee S only contains four characters: "[", "]", "(", ")"
-
Output
-
The output of each group of input data occupies one row. If the brackets contained in the string are paired, Yes is output. If the string is not paired, No is output.
-
Sample Input
-
3[(])(])([[]()])
-
Sample output
-
NoNoYes
C ++ version:
#include<cstdio>#include<cstring>using namespace std;struct stack{char a[10001];int top;}b;char d[10001];void push(char c){b.a[b.top]=c;b.top++;}void pop(char c){b.top--;}int main(){int n;scanf("%d",&n);//cin>>n;while(n--){scanf("%s",d);//cin>>d;b.top=0;push(d[0]);for(int i=1; i<strlen(d); i++){if(d[i]==')'&&b.a[b.top-1]=='(' || d[i]=='}'&&b.a[b.top-1]=='{' || d[i]==']'&&b.a[b.top-1]=='[' ){pop(d[i]);}else{push(d[i]);}}if(b.top==0){printf("Yes\n");//cout<<"Yes"<<endl;}else{printf("No\n");//cout<<"No"<<endl;}}return 0;}
Java version:
import java.util.Scanner;public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);int num = input.nextInt();int i=0; String arr[] = new String[num];for(i=0; i<num; i++){arr[i] = input.next();}i=0;while(i < num){String flaot = "No";int b = to_make(arr[i]);if(b == 1){flaot = "Yes";}System.out.println(flaot);i++;}}public static int to_make(String s){int slen = s.length();if(slen%2 != 0 && slen > 1) return 0;if(s.charAt(0) == ')'||s.charAt(0) == '}'||s.charAt(0) == ']') return 0;char []ch = new char [slen/2];int j=0; char CA = '-';for(int i=0; i<slen; i++){char ca = s.charAt(i);if(ca == '('||ca == '{'||ca == '['){ch[j] = ca;}else{if(j<=0) return 0;if(ca == ')'){CA = '(';}else if(ca == '}'){CA = '{';}else if(ca == ']'){CA = '[';}if(ch[j-1] != CA) return 0;else{j -= 2;}}j++;}if(j != 0) {return 0;}return 1;}}
Java stack application:
Import java. util. imports; import java. util. stack; public class kuohao {public static void main (String [] args) {consumer SC = new consumer (System. in); int n = SC. nextInt (); // 3 test data stacks <Character> Stack = null; while (n! = 0) {// read a test String [] () [(]) String str = SC from the console. next (); // if the input string is an odd number, it does not match if (str. length () % 2 = 1) {System. out. println ("No");} else {// indicates that the Character is an even number of stacks = new stack <Character> (); // traverses the first test string [] () [(]) for (int I = 0; I <str. length (); I ++) {if (stack. isEmpty () {// If the stack is empty. push (str. charAt (I);} else if (stack. peek () = '[' & str. charAt (I) = ']' | stack. peek () = '(' & str. charAt (I) = ') {// It indicates that the characters in the stack are not empty at this time, and they are consistent. stack. pop ();} else {stack. push (str. charAt (I) ;}} if (stack. isEmpty () {// If the stack is empty, the brackets match System. out. println ("Yes");} else {// the stack is not empty and the brackets do not match System. out. println ("No") ;}} n --;}}}