[BOJ][1406] 에디터
Updated:
1. 문제 링크
https://www.acmicpc.net/problem/1406
2. 사용 알고리즘
스택
3. 풀이
- 
    
커서를 기준으로 왼쪽과 오른쪽의 값을 저장하기 위한 s1, s2 두 개의 스택 선언
 - 
    
입력받은 문자열을 s1 스택에 차례대로 push
 - 
    
명령어 처리
L 명령어 : s1 스택의 top 값을 s2 스택에 push 후 s1 스택 popD 명령어 : s2 스택의 top 값을 s1 스택에 push 후 s2 스택 popB 명령어 : s1 스택 popP 명령어 : s1 스택에 push - 
    
모든 명령어 처리가 끝나면, s1 스택의 모든 값을 s2 스택으로 옮김
 - 
    
s2 스택의 값을 차례대로 출력
 
4. 소스 코드
4-1. C++
https://github.com/dev-aiden/problem-solving/blob/main/boj/1406.cpp
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
#include <iostream>
#include <stack>
using namespace std;
stack<char> s1, s2;
int main(void) {
    ios_base::sync_with_stdio(false);
    string s; cin >> s;
    int len = s.length();
    for (int i = 0; i < len; ++i) s1.push(s[i]);
    int n; for (cin >> n; n--;) {
        char cmd; cin >> cmd;
        if (cmd == 'L') {
            if (!s1.empty()) {
                s2.push(s1.top());
                s1.pop();
            }
        } else if (cmd == 'D') {
            if (!s2.empty()) {
                s1.push(s2.top());
                s2.pop();
            }
        } else if (cmd == 'B') {
            if (!s1.empty()) {
                s1.pop();
            }
        } else if (cmd == 'P') {
            char ch; cin >> ch;
            s1.push(ch);
        }
    }
    while (!s1.empty()) {
        s2.push(s1.top());
        s1.pop();
    }
    while (!s2.empty()) {
        cout << s2.top();
        s2.pop();
    }
    cout << "\n";
    return 0;
}
4-2. JAVA
https://github.com/dev-aiden/problem-solving/blob/main/boj/1406.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
    static Stack<Character> s1 = new Stack<>();
    static Stack<Character> s2 = new Stack<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String s = br.readLine();
        int len = s.length();
        for(int i = 0; i < len; ++i) s1.push(s.charAt(i));
        int n = Integer.parseInt(br.readLine());
        for(int i = 0; i < n; ++i) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            String cmd = st.nextToken();
            if(cmd.charAt(0) == 'L') {
                if(!s1.empty()) {
                    s2.push(s1.peek());
                    s1.pop();
                }
            } else if(cmd.charAt(0) == 'D') {
                if(!s2.empty()) {
                    s1.push(s2.peek());
                    s2.pop();
                }
            } else if(cmd.charAt(0) == 'B') {
                if(!s1.empty()) {
                    s1.pop();
                }
            } else if(cmd.charAt(0) == 'P') {
                s1.push(st.nextToken().charAt(0));
            }
        }
        while(!s1.empty()) {
            s2.push(s1.peek());
            s1.pop();
        }
        while(!s2.empty()) {
            sb.append(s2.peek());
            s2.pop();
        }
        sb.append("\n");
        System.out.println(sb);
    }
}
      
Leave a comment