CT

[BOJ] 18단계 스택

kinggora 2023. 4. 13. 20:41
1 10828 스택
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        CustomStack stack = new CustomStack();
        for(int i = 0; i < n; i++) {
            String[] split = reader.readLine().split(" ");
            switch (split[0]) {
                case "push":
                    stack.push(Integer.parseInt(split[1]));
                    break;
                case "pop":
                    System.out.println(stack.pop());
                    break;
                case "size":
                    System.out.println(stack.size());
                    break;

                case "empty":
                    System.out.println(stack.empty());
                    break;
                case "top":
                    System.out.println(stack.top());
                    break;
            }
        }

    }

    private static class CustomStack {
        private final List<Integer> store = new ArrayList<>();

        public void push(int X) {
            store.add(X);
        }

        public int pop() {
            if(store.isEmpty()) {
                return -1;
            } else {
                return store.remove(store.size() - 1);
            }
        }

        public int size() {
            return store.size();
        }

        public int empty() {
            if(store.isEmpty()) {
                return 1;
            } else {
                return 0;
            }
        }

        public int top() {
            if(store.isEmpty()) {
                return -1;
            } else {
                return store.get(store.size() - 1);
            }
        }
    }
}

*스택 -> ArrayList로 구현

 

2 10773 제로
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int k = Integer.parseInt(reader.readLine());
        int[] stack = new int[k+1];
        int top = -1;
        for(int i = 0; i < k; i++) {
            int num = Integer.parseInt(reader.readLine());
            if(num == 0) {
                stack[top] = 0;
                top--;
            } else {
                top++;
                stack[top] = num;
            }
        }
        int sum = 0;
        for(int i = 0; i <= top; i++) {
            sum += stack[i];
        }
        System.out.println(sum);
    }
}

*스택 -> 배열로 구현

 

3 9012 괄호

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < T; i++) {
            List<String> stack = new ArrayList<>();
            String str = reader.readLine();
            boolean underflow = false;
            for(int j = 0; j < str.length(); j++) {
                if(str.charAt(j) == '(') {
                    stack.add("(");
                } else if(stack.isEmpty()) {
                    underflow = true;
                    break;
                } else {
                    stack.remove(stack.size() - 1);
                }
            }
            if(!underflow && stack.isEmpty()) {
                sb.append("YES");
            } else {
                sb.append("NO");
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

*underflow 고려

 

4 4949 균형잡힌 세상
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        while(true) {
            String str = reader.readLine();
            if(str.equals(".")) {
                break;
            }
            List<String> stack = new ArrayList<>();
            boolean isValanced = true;
            for(int i = 0; i < str.length(); i++) {
                String s = String.valueOf(str.charAt(i));
                if(!"([)]".contains(s)) {
                    continue;
                }
                if("([".contains(s)) {
                    stack.add(s);
                } else if(stack.isEmpty()) {
                    isValanced = false;
                    break;
                } else {
                    String element = stack.remove(stack.size() - 1);
                    if(s.equals(")") && !element.equals("(")){
                        isValanced = false;
                        break;
                    } else if(s.equals("]") && !element.equals("[")){
                        isValanced = false;
                        break;
                    }
                }
            }
            if(isValanced && stack.isEmpty()) {
                sb.append("yes");
            } else {
                sb.append("no");
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

 

5 1874 스택 수열
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());

        int[] sequence = new int[n];
        for(int i = 0; i < n; i++) {
            sequence[i] = Integer.parseInt(reader.readLine());
        }

        CustomStack stack = new CustomStack();
        StringBuilder sb = new StringBuilder();
        int index = 0;
        int value = 1;
        while(index < n) {
            if(sequence[index] != stack.top()) {
                if(value > n) {
                    System.out.println("NO");
                    return;
                } else {
                    stack.push(value);
                    value++;
                    sb.append("+").append("\n");
                }
            } else {
                stack.pop();
                index++;
                sb.append("-").append("\n");
            }
        }
        System.out.print(sb);
    }

    static class CustomStack {
        List<Integer> stack = new ArrayList<>();

        public void push(int x) {
            stack.add(x);
        }

        public int pop() {
            if(stack.isEmpty()) {
                return -1;
            }
            return stack.remove(stack.size() - 1);
        }

        public int top() {
            if(stack.isEmpty()) {
                return -1;
            }
            return stack.get(stack.size() - 1);
        }
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        CustomStack stack = new CustomStack();
        StringBuilder sb = new StringBuilder();

        int value = 1;
        for(int i = 0; i < n; i++) {
            int input = Integer.parseInt(reader.readLine());
            if(stack.top() != input && value <= n) {
                while(value <= input) {
                    stack.push(value);
                    value++;
                    sb.append("+").append("\n");
                }
            } else if(stack.isEmpty()){
                System.out.println("NO");
                return;
            }
            if(stack.top() == input){
                stack.pop();
                sb.append("-").append("\n");
            }
        }
        if(stack.isEmpty()) {
            System.out.print(sb);
        } else {
            System.out.println("NO");
        }
    }

    static class CustomStack {
        List<Integer> stack = new ArrayList<>();

        public void push(int x) {
            stack.add(x);
        }

        public int pop() {
            if(stack.isEmpty()) {
                return -1;
            }
            return stack.remove(stack.size() - 1);
        }

        public int top() {
            if(stack.isEmpty()) {
                return -1;
            }
            return stack.get(stack.size() - 1);
        }

        public boolean isEmpty() {
            if(stack.isEmpty()) {
                return true;
            }
            return false;
        }
    }
}