728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Stack; public class Main { private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); private static String check(String input) { Stack<Character> st = new Stack<>(); for(int i=0; i<input.length()-1; i++) { if(input.charAt(i)=='(') st.push('('); else if(input.charAt(i)=='[') st.push('['); else if(input.charAt(i)==')') { if(st.isEmpty() || st.peek()!='(') return "no"; else { st.pop(); } } else if(input.charAt(i)==']') { if(st.isEmpty() || st.peek()!='[') return "no"; else { st.pop(); } } } if(st.isEmpty()) return "yes"; else return "no"; } public static void main(String[] args) throws IOException{ StringBuilder sBuilder = new StringBuilder(); while(true) { String input = br.readLine(); if(input.equals(".")) break; sBuilder.append(check(input)+"\n"); } bw.write(sBuilder.toString()); bw.flush(); bw.close(); } } | cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
[백준][Java] 5430번 AC (String, Deque) (0) | 2021.09.09 |
---|---|
[백준][Java] 1032번 명령 프롬프트 (String) (0) | 2021.09.09 |
[백준][Java] 11404번 플로이드 (플로이드 와샬, DP) (0) | 2021.09.08 |
[백준][Java] 5430번 AC (0) | 2021.07.17 |
[백준][Java] 10773번 제로 (0) | 2021.07.17 |