标签:style blog color ar java for sp 数据 div
package practice; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Stack; /** * 描述 现在,有一行括号序列,请你检查这行括号是否配对。<br> * 输入第一行输入一个数N(0<N<=100),表示有N组测试数据。<br> * 后面的N行输入多组输入数据,每组输入数据都是一个字符串S (S的长度小于10000,且S不是空串 ),测试数据组数少于5组。<br> * 数据保证S中只含有"[","]","(",")"四种字符输出每组输入数据的输出占一行,如果该字符串中所含的括号是配对的 * ,则输出Yes,如果不配对则输出No * * @author caiyu * @date 2014-10-22 */ public class Main { public static void main(String[] args) { char c, start; boolean f; String str; int i, j, t, len; List<String> rl = new ArrayList<String>(); Stack<Character> stack = new Stack<Character>(); Scanner s = new Scanner(System.in); t = s.nextInt(); long time = System.currentTimeMillis(); for (i = 0; i < t; i++) { str = s.next(); f = true; for (j = 0, len = str.length(); j < len; j++) { c = str.charAt(j); if (c == 91 || c == 40) stack.add(str.charAt(j)); else { if (stack.size() == 0) f = false; else { start = stack.pop(); f = start == 40 ? c == 41 : (start == 91 ? c == 93 : false); } } if (!f) break; } if (stack.size() > 0) f = false; stack.clear(); if (f) rl.add("YES"); else rl.add("NO"); } for (String r : rl) { System.out.println(r); } System.out.println(System.currentTimeMillis() - time); } }
标签:style blog color ar java for sp 数据 div
原文地址:http://www.cnblogs.com/anrainie/p/4042348.html