백준 28278번 스택2
import java.util.Scanner;
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Stack<Integer> store = new Stack<>();
        StringBuilder output = new StringBuilder();
        int size = in.nextInt();
        for (int i = 0; i < size; i++) {
            int number = in.nextInt();
            
            if (number == 1) {
                int input = in.nextInt();
                store.push(input);
            }
            
            if (number == 2) {
                if (!store.isEmpty()) {
                    output.append(store.pop()).append("\n");
                } else {
                    output.append("-1\n");
                }
            }
            
            if (number == 3) {
                output.append(store.size()).append("\n");
            }
            
            if (number == 4) {
                if (store.isEmpty()) {
                    output.append("1\n");
                } else {
                    output.append("0\n");
                }
            }
            
            if (number == 5) {
                if (!store.isEmpty()) {
                    output.append(store.peek()).append("\n");
                } else {
                    output.append("-1").append("\n");
                }
            }
        }
        System.out.println(output.toString());
    }
}
백준 4949번 (균형잡힌 세상)
import java.util.Scanner;
import java.util.Stack;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            String words = in.nextLine();
            if (words.equals(".")) break;
            System.out.println(answer(words));
        }
    }
    private static String answer(String words) {
        Stack<Character> store = new Stack<>();
        for (char word : words.toCharArray()) {
            
            if (word == '(' || word == '[') {
                store.push(word);
            }
            
            else if (word == ')') {
                if (store.isEmpty() || store.peek() != '(') {
                    return "no";
                } else {
                    store.pop();
                }
            } else if (word == ']') {
                if (store.isEmpty() || store.peek() != '[') {
                    return "no";
                } else {
                    store.pop();
                }
            }
        }
        
        
        if (store.isEmpty()) {
            return "yes";
        } else {
            return "no";
        }
    }
}