import java.util.Scanner; import java.util.Stack; public class Test15 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.nextLine(); Stack<Character> s = new Stack<Character>(); if(str.length() % 2 != 0){ System.out.println(str+" -> "+"FALSE"); }else{ boolean flag = true; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch=='('||ch=='['||ch=='{'){ switch(ch){ case '(': s.push(')');break; case '[': s.push(']');break; case '{': s.push('}');break; } } else if(ch==')'||ch==']'||ch=='}'){ if(s.isEmpty()||ch!=s.peek()){ flag = false; System.out.println(str+" -> "+"FALSE"); break; }else{ s.pop(); } } } if(s.isEmpty()) System.out.println(str+" -> "+"TRUE"); else if(flag) System.out.println(str+" -> "+"FALSE"); } } } } /* () [()] ()() ({}([])) ) ({ {)(} */
括号匹配
Jumping from failure to failure with undiminished enthusiasm is the big secret to success.