1406編集(C++)



#include <iostream>
#include<stack>
using namespace std;

int main() {
    char command;
    string N;
    int T;
    cin >> N >> T;
    stack<char> mine_right;
    stack<char> mine_left;
    for (int i = 0; i < N.length(); i++) {
        mine_left.push(N[i]);
    }
    for (int i = 0; i < T; i++) {
        cin >> command;
        if (command == 'L') {
            if (!mine_left.empty()) {
                mine_right.push(mine_left.top());
                mine_left.pop();
            }
        }
        else if (command == 'D') {
            if (!mine_right.empty()) {
                mine_left.push(mine_right.top());
                mine_right.pop();
            }
        }
        else if (command == 'B') {
            if (!mine_left.empty()) mine_left.pop();
        }
        else if (command == 'P') {
            char $;
            cin >> $;
            mine_left.push($);
        }
    }
    while (!mine_left.empty()) {
        mine_right.push(mine_left.top());
        mine_left.pop();
    }
    while (!mine_right.empty()) {
        cout<< mine_right.top();
        mine_right.pop();
    }
        
}
スタックの説明を参照してください.
https://velog.io/@kimeunseo58/%EC%8A%A4%ED%83%9D
スタックソース:
https://velog.io/@kimeunseo58/10828%EC%8A%A4%ED%83%9DC