문제 링크: https://www.acmicpc.net/problem/5430
dq를 사용해서 풀었다.
입력을 받는 부분을 조금 처리를 해줘야 됐다. ]부분과 , 부분이 나왔을 때, dq에 넣어주는 일을 해야됐다.
명령어에서 reverse하는 것이 나오면, flag를 사용하여, reverse를 해줘야된다고 체크를 하고, 해당 flag가 true일때는 dq의 맨끝 부분이 앞부분이라고 생각하고 구하면 된다.
#include <iostream>
#include <deque>
#include <string>
using namespace std;
string cmd;
deque<int> dq;
int n;
bool isReverse;
bool doCmd(){
for(int i = 0 ; i < cmd.length() ; i++){
if(cmd[i] == 'R') isReverse = (isReverse) ? false : true;
else{
if(dq.empty()) return false;
if(isReverse){
dq.pop_back();
}
else{
dq.pop_front();
}
}
}
return true;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int testcase;
char temp;
cin >> testcase;
while(testcase--){
isReverse = false;
dq.clear();
cin >> cmd;
cin >> n;
string num = "";
while(1){
cin >> temp;
if(temp == ']') {
if(num != ""){
dq.push_back(stoi(num));
}
break;
}
else if(temp == '[') continue;
else if(temp == ','){
dq.push_back(stoi(num));
num = "";
}
else{
num += temp;
}
}
if(doCmd()){
cout << "[";
if(isReverse){
for(int i = dq.size() - 1 ; i >= 0 ; i--){
cout << dq[i];
if(i != 0) cout << ",";
}
}
else{
for(int i = 0 ; i < dq.size() ; i++){
cout << dq[i];
if(i != dq.size()-1) cout << ",";
}
}
cout << "]\n";
}
else{
cout << "error\n";
}
}
}