https://www.acmicpc.net/problem/5430
풀이
배열을 뒤집을 필요 없이, 덱은 양 끝에서 pop이 자유로우므로 그 점을 사용해서 direction만 지정해주면 된다.
배열을 string으로 받을 때, 숫자가 두 자리수 이상이 될 때를 예외처리 해줘야 한다.
/*boj g5 5430 AC*/
#include <iostream>
#include <deque>
#include <string>
#define MAXN 100005
using namespace std;
int T, N;
string P;
string arr;
deque<int> DQ;
void strToDQ()
{
DQ.clear();
string str = "";
for (int i = 1; i < arr.size(); i++)
{
if (arr[i] == ']' || arr[i] == ',')
continue;
while (i < arr.size() && arr[i] != ',' && arr[i] != ']')
{
str += arr[i];
i++;
}
DQ.push_back(stoi(str));
str = "";
}
}
int DCnt()
{
int cnt = 0;
for (int i = 0; i < P.size(); i++)
{
if (P[i] == 'D')
cnt++;
}
return cnt;
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--)
{
cin >> P >> N >> arr;
if (DCnt() > N)
{
cout << "error\n";
continue;
}
strToDQ();
int dir = 0;
for (int p = 0; p < P.size(); p++)
{
if (P[p] == 'R')
{
dir = dir ? 0 : 1;
}
else
{
if (dir == 0)
{
DQ.pop_front();
}
else
{
DQ.pop_back();
}
}
}
// 출력
cout << "[";
if (dir == 0)
{
while (!DQ.empty())
{
cout << DQ.front();
if (DQ.size() != 1)
cout << ",";
DQ.pop_front();
}
}
else
{
while (!DQ.empty())
{
cout << DQ.back();
if (DQ.size() != 1)
cout << ",";
DQ.pop_back();
}
}
cout << "]\n";
}
}
'알고리즘' 카테고리의 다른 글
백준 g4 불 c++ (0) | 2024.01.12 |
---|---|
백준 s1 1926 그림 c++ (0) | 2024.01.12 |
백준 s4 1021 회전하는 큐 c++ (0) | 2024.01.11 |
백준 s4 2164 카드2 c++ (0) | 2024.01.10 |
백준 g5 2493 탑 c++ (0) | 2024.01.10 |