Algorithm/C++ - BOJ

BOJ/백준5430 AC C++

ㅇㅇ잉 2021. 1. 25. 23:53

이거는 다시 풀어봐야겠다.

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

int main() {
	deque <int> dq;
	int T, N;
	string func; //함수
	string arr; //숫자 배열
	string tmp; //숫자 옮길 때 사용할 string 변수

	cin >> T;

	while (T--) {

		cin >> func >> N >> arr;

		for (int i = 0; i < arr.length(); i++) {
			if (arr[i] == '[')
				continue;
			else if ('0' <= arr[i] && arr[i] <= '9')
				tmp += arr[i];
			else if (arr[i] == ',' || arr[i] == ']')

			{
				if (!tmp.empty())
				{
					dq.push_back(stoi(tmp));
					tmp.clear();
				}
			}
		}
		bool err = false; //error일 경우 true
		bool R = false; //뒤집어져 있으면 true

		for (int i = 0; i < func.length(); i++) {
			if (func[i] == 'R') R = !R;
			else {
				if (dq.empty()) {
					err = true;
					cout << "error\n";
					break;
				}
				else {
					if (R == false) dq.pop_front();
					else dq.pop_back();
				}
			}
		}

		if (!err)
		{
			if (!R)
			{
				cout << "[";
				while (!dq.empty())
				{
					cout << dq.front();
					dq.pop_front();
					if (!dq.empty())
						cout << ",";
				}
				cout << "]\n";
			}
			else
			{
				cout << "[";
				while (!dq.empty())
				{
					cout << dq.back();
					dq.pop_back();
					if (!dq.empty())
						cout << ",";
				}
				cout << "]\n";
			}
		}
	}
		return 0;
}