編集

2987 ワード

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <cmath>
using namespace std;


int main() {
	//freopen("input.txt", "rt", stdin);
	stack<char> left;
	stack<char> right;
	string str;
	cin >> str;
	for (int i = 0; i < str.length(); i++) {
		left.push(str[i]);
	}
	int n;
	cin >> n;
	char m;
	for (int j = 0; j < n; j++) {

		cin >> m;
		if (m == 'L') {
			if (left.size() == 0) {
				continue;
			}
			char top = left.top();
			left.pop();
			right.push(top);
		}
		else if (m == 'D') {
			if (right.size() == 0) {
				continue;
			}
			char top = right.top();
			right.pop();
			left.push(top);
		}
		else if (m == 'B') {
			if (left.size() == 0) {
				continue;
			}
			left.pop();
		}
		else if (m == 'P') {
			char ex;
			cin >> ex;
			left.push(ex);
		}
		
		
	}
	int num = left.size();
	for (int i = 0; i < num; i++) {
		right.push(left.top());
		left.pop();
	}
	num = right.size();
	for (int i = 0; i < num; i++) {
		cout << right.top();
		right.pop();
	}
	

	return 0;
}