#include <iostream>
#include <list>

using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    string str;
    cin >> str;
    list<char>  L;
    for(auto c:str) L.push_back(c);
    auto t =L.end();
    int n;
    cin >> n;
    while(n--) {
        char word,alp;
        cin >> word;
        if(word=='P') {
            cin >> alp;
            L.insert(t,alp);
        } else if(word=='L') {
            if(t!=L.begin()) t--;
        } else if(word=='D') {
            if(t!=L.end()) t++;
        } else if(word=='B') {
            if(t!=L.begin()) {
                t--;
                t=L.erase(t);
            }
        }
    }
    for(auto c:L) cout << c;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: