백준

[백준] 5430번 AC (C++ , DEQUE)

해랑쓰 2021. 10. 5. 14:37
728x90
반응형

뒤집힌 상태를 나타내는 BOOL 변수 HEAD를 가지고 뒤집힌 상태면 뒤를 삭제하고 아니면 앞을 삭제한다.

속도를 위해 deque을 사용한다.

 

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <string>
#include <deque>
using namespace std;



int main(void) {

	ios::sync_with_stdio(false);
	cin.tie(NULL);

	freopen("input.txt","r",stdin);

	int t,n;
	cin >> t;
	string p;
	for (int tc = 0; tc < t; tc++) {
		deque<int> dq;
		cin >> p>>n;
		string arr;
		cin >> arr;
		string tmp = "";
		for (int i = 0; i < arr.length(); i++) {
			if (arr[i] == ',' || arr[i] == '[' || arr[i] == ']') {
				if (tmp == "")continue;
				dq.push_back(stoi(tmp));
				tmp = "";
				continue;
			}
			tmp += arr[i];
		}
		bool head = true;
		bool error = false;
		for (int k = 0; k < p.length(); k++) {
			if (p[k] == 'R') {
				head = !head;
				
			}
			else {
				if (dq.empty()) {
					error = true;
					break;
				}
				if (head) {
					dq.pop_front();
				}
				else {
					dq.pop_back();
				}
			}
		}
		if (error) {
			cout << "error" << "\n";
		}
		else {
			cout << "[";
			while (!dq.empty()) {
				if (head) {
					cout << dq.front();
					if (dq.size() > 1) {
						cout << ",";
					}
					dq.pop_front();
				}
				else {
					cout << dq.back();
					if (dq.size() > 1) {
						cout << ",";
					}
					dq.pop_back();
				}
				
			}
			cout << "]\n";
		}

	}


	return 0;
}
728x90
반응형