很多时候,人生如同一个梦,不断的老去,不断的逝去。
http://acm.nyist.net/JudgeOnline/problem.php?pid=2
数据结构中的经典题目,栈的使用那一章,如果是左括号入站,右括号呢,跟该右括号匹配的一定是栈顶元素,如果此时栈顶为空或元素不匹配,则失败
1 import java.util.Scanner; 2 import java.util.Stack; 3 4 5 public class Main { 6 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 Scanner scn=new Scanner(System.in); 10 int len=scn.nextInt(); 11 while(len-->0) 12 { 13 14 System.out.println(pipei(scn.next())); 15 } 16 17 18 } 19 public static String pipei(String s) 20 { 21 char c[]=s.toCharArray(); 22 Stack<Character> stack=new Stack<Character>(); 23 for(int i=0;i<s.length();i++) 24 { 25 if(c[i]=='('||c[i]=='[') 26 { 27 stack.push(c[i]); 28 } 29 else 30 { 31 if(stack.isEmpty()) 32 { 33 return "No"; 34 35 } 36 else 37 { 38 Character t=stack.peek(); 39 if((t=='('&&c[i]==')')||(t=='['&&c[i]==']')) 40 { 41 stack.pop(); 42 } 43 else return "No"; 44 45 46 47 48 } 49 50 51 52 53 } 54 55 56 57 } 58 59 if(stack.isEmpty()) return "Yes"; 60 else return "No"; 61 62 63 64 } 65 66 }