'PS(Problem Solving)/Stack'에 해당되는 글 1건

  1. 2019.03.03 [BOJ]10828. 스택
posted by 귀염둥이채원 2019. 3. 3. 22:10

# 문제링크

https://www.acmicpc.net/problem/10828


# 문제

정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.


명령은 총 다섯 가지이다.

push X: 정수 X를 스택에 넣는 연산이다.

pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.

size: 스택에 들어있는 정수의 개수를 출력한다.

empty: 스택이 비어있으면 1, 아니면 0을 출력한다.

top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.


# 풀이1

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
57
58
59
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;
 
public class Main {
    static int N;
    static Stack <Integer> stk;
    
    static void func(String inputStr) {
        String strCommand[];
        if (inputStr.contains("push")) {
            strCommand = inputStr.split(" ");
            int number = Integer.parseInt(strCommand[1]);
            stk.push(number);
        }
        else if (inputStr.equals("pop")) {
            if (stk.size() > 0) {
                System.out.println(stk.pop());
            }
            else {
                System.out.println(-1);
            }
        }
        else if (inputStr.equals("size")) {
            System.out.println(stk.size());
        }
        else if (inputStr.equals("empty")) {
            if (stk.isEmpty()) {
                System.out.println(1);
            }
            else {
                System.out.println(0);
            }
        }
        else if (inputStr.equals("top")) {
            if (stk.isEmpty()) {
                System.out.println(-1);
            }
            else {
                System.out.println(stk.peek());
            }
        }
    }
    
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
 
        stk = new Stack <Integer>();
        stk.clear();
        
        N = sc.nextInt();
 
        for (int i=0; i<=N; i++) {
            func(sc.nextLine());
        }
        sc.close();
    }
}
cs


# 풀이2

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
57
58
59
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
 
public class Main {
    static int N;
    static Stack <Integer> stk;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        stk = new Stack <Integer>();
        stk.clear();
        
        N = Integer.parseInt(br.readLine());
        for (int i=0; i<N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            
            switch (st.nextToken()) {
                case "push":
                    stk.push(Integer.parseInt(st.nextToken()));
                    break;
                case "pop":
                    if (stk.size() > 0) {
                        sb.append(stk.pop() + "\n");
                    }
                    else {
                        sb.append("-1" + "\n");
                    }
                    break;
                case "size":
                    sb.append(stk.size() + "\n");
                    break;
                case "empty":
                    if (stk.isEmpty()) {
                        sb.append(1 + "\n");
                    }
                    else {
                        sb.append(0 + "\n");
                    }
                    break;
                case "top":
                    if (stk.isEmpty()) {
                        sb.append(-1 + "\n");
                    }
                    else {
                        sb.append(stk.peek() + "\n");
                    }
                    break;
            }
        }
        
        System.out.println(sb);
    }
}
cs


# 회고

풀이 2에서 BufferedReader, StringBuilder를 사용해서 성능을 개선하였다.

(풀이1: 140ms, 풀이2: 436ms)


split 대신 StringTokenizer를 사용하여 문자열을 파싱함


# 풀이 참고

https://www.acmicpc.net/source/10582053

https://www.acmicpc.net/source/7385991

https://www.acmicpc.net/source/8726490