[C++/백준] 1406 에디터

9703 단어 백준백준

#1406 에디터

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

📝문제 포인트

  1. 중간에 문자를 삽입할 수 있어야한다.
    ->배열로 푸는 건 비효율적임
  2. stack과 queue를 사용할 경우 두 개씩 사용한다.
  3. list를 사용할 수도 있다.(중간 삽입이 자유롭기때문)

✍어떻게 풀었나요

앞 뒤로 삽입 삭제가 가능한 deque를 사용했습니다
deque두 개를 두고, 두 개 사이에 커서가 있다고 생각함


✍코드

#include <iostream>
#include <string>
#include <deque>
#include <algorithm>
using namespace std;

int main() {
    string str;
    cin >> str;

    deque<char> q1,q2;
    for (int i = 0; i < str.length(); i++) {
        q1.push_back(str[i]);
    }

    int num;
    cin >> num;
    for (int i = 0; i < num; i++) {
        char testCh;
        cin >> testCh;

        if (testCh == 'P') {
            char x;
            cin >> x;
            q1.push_back(x);
        }
        else if (testCh == 'L') {
            if (!q1.empty()) {
                q2.push_front(q1.back());
                q1.pop_back();
            }
        }
        else if (testCh == 'B') {
            if (!q1.empty()) {
                q1.pop_back();
            }
        }
        else { //D
            if (!q2.empty()) {
                q1.push_back(q2.front());
                q2.pop_front();
            }
        }
    }
    while (!q1.empty()) {
        cout << q1.front();
        q1.pop_front();
    }
    while (!q2.empty()) {
        cout << q2.front();
        q2.pop_front();
    }
}

좋은 웹페이지 즐겨찾기