南阳理工OJ---括号配对问题

import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Stack stack = null;
		while (n != 0) {
			String str = sc.next();
			if (str.length() % 2 == 1) {
				System.out.println("No");
			} else {
				stack = new Stack();
				for (int i = 0; i < str.length(); i++) {
					if (stack.isEmpty()) {
						stack.push(str.charAt(i));
					} else if (stack.peek() == '[' && str.charAt(i) == ']'
							|| stack.peek() == '(' && str.charAt(i) == ')') {
						stack.pop();
					} else {
						stack.push(str.charAt(i));
					}
				}
				System.out.println(stack.isEmpty() ? "Yes" : "No");
			}
			n--;
		}

	}
}

你可能感兴趣的:(南阳理工OJ---括号配对问题)