#include <iostream>
#include <iterator>
#include <string>
#include <array>
#include <list>
using namespace std;

list<char> editor;

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    string input_str;
    cin >> input_str;
    for (int i  = 0; i < input_str.size(); i++) {
        editor.push_back(input_str[i]);
    }

    auto current_idx = editor.end();
    int cnt;
    cin >> cnt;
    while (cnt-- > 0) {
        char command;
        cin >> command;
        char append_char;
        switch (command) {
        case 'P':
            cin >> append_char;
            current_idx = editor.insert(current_idx, append_char);
            current_idx++;
             break;

        case 'B':
            if (editor.begin() == current_idx) {
                break;
            }

            --current_idx;
            current_idx = editor.erase(current_idx);
            break;
        case 'D':
            if (editor.end() == current_idx) {
                break;
            }

            ++current_idx;
            break;
        case 'L':
            if (editor.begin() == current_idx) {
                break;
            }

            --current_idx;
            break;

        default:
            break;
        }
    }

    for (auto& i : editor) {
        cout << i;
    }
    cout << '\n';
    return 0;
}

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

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net

 

문제 descption을 읽어보면 전형적인 linked list 동작이었습니다.

처음엔 string 객체를 이용해 문제를 풀었으나, 시간 초과를 초래했네요.

그래서 std::list로 컨테이너를 교체했습니다.

'문제풀이 > 백준' 카테고리의 다른 글

5430 - AC  (0) 2023.01.05
6603 - 로또  (0) 2023.01.03
10828 - 스택  (0) 2022.12.31
2628 - 종이 자르기  (0) 2022.12.07
1244 - 스위치 켜고 끄기  (0) 2022.09.09

+ Recent posts