Little Jay

[C++] 백준 5430번 틀 본문

알고리즘/BOJ

[C++] 백준 5430번 틀

Jay, Lee 2021. 8. 20. 16:12
#include <iostream>
#include <string>
#include <deque>
using namespace std;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;

	bool check, err;
	string s, numline;
	int n;

	while (t--) {
		deque<int> dq;
		check = true;
		err = false;
		cin >> s >> n >> numline;
		int i = 1;
		while (numline[i] != '\0') {
			int x = 0;
			while (numline[i] >= '0' && numline[i] <= '9') {
				x *= 10;
				x += int(numline[i] - '0');
				i++;
			}
			if (x != 0) {
				dq.push_back(x);
			}
			i++;
		}

		i = 0;
		while (s[i] != '\0') {
			if (s[i] == 'R') {
				check = !check;
			}
			else if (s[i] == 'D') {
				if (dq.empty()) {
					cout << "error" << "\n";
					err = true;
					break;
				}
				if (check) {
					dq.pop_front();
				}
				else {
					dq.pop_back();
				}
			}
			i++;
		}

		if (!err) {
			cout << "[";
		}
		while (!dq.empty()) {
			if (check) {
				auto c = dq.front();
				dq.pop_front();
				cout << c;
			}
			else {
				auto c = dq.back();
				dq.pop_back();
				cout << c;
			}
			if (!dq.empty())
				cout << ", ";
		}
		if (!err) {
			cout << "]" << "\n";
		}


	}


	return 0;
}
Comments