알고리즘 풀이/백준
[백준][Java] 3986번 좋은 단어
배게
2019. 3. 24. 11:16
728x90
Stack문제인줄 몰랐고
아치형으로 어떻게 그리는 줄 몰랐음 A는 A끼리 B는 B끼리 전부 연결해야하는 것인줄 알고
문제 파악에 실수함
AA 1쌍씩 BB1쌍씩 묶어주면 됨 stack으로 계속집어넣어주면서 쌍만들어준 후에
for문 통과했을 때 stack이 싹 비어있으면 문제 해결
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 | import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); sc.nextLine(); int result=0; for(int i=0;i<N;i++) { String s = sc.nextLine(); Stack<Character> stack = new Stack<>(); int j=0; for(; j<s.length(); j++) { if(!stack.isEmpty() && stack.peek() == s.charAt(j)) { stack.pop(); } else stack.push(s.charAt(j)); } if(stack.size()==0) result++; } System.out.println(result); } } |