[ 문제 ]
[ 접근방법 ]
string input의 경우 [ 24.05.04 ] c++ string input (tistory.com) 를 참고한다.
배열의 양끝 인덱스를 int 변수에 저장하고, 에러가 발생했는지 뒤집었는지를 bool 변수로 저장한다.
입력에 따라 변수 값만 조정하기에 O(n)으로 가능하다. ( n <= 700,000 )
[ 소스코드 ]
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int t, n, arr[100010];
string p, xx;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--)
{
cin >> p >> n >> xx;
xx = xx.substr(1, xx.length() - 2);
bool isSuccess = true, isReverse = false;
int l = 0, r = 0;
istringstream ss(xx);
string stringBuffer;
while (getline(ss, stringBuffer, ','))
{
arr[r++] = stoi(stringBuffer);
}
for (int i = 0; i < p.length(); i++)
{
if (p[i] == 'R')
{
isReverse = !isReverse;
}
if (p[i] == 'D')
{
if (l == r)
{
isSuccess = false;
break;
}
else if (isReverse)
{
r--;
}
else
{
l++;
}
}
}
if (isSuccess)
{
cout << "[";
if (isReverse)
{
for (int i = r - 1; i >= l; i--)
{
cout << arr[i];
if (i > l)
cout << ",";
}
}
else
{
for (int i = l; i < r; i++)
{
cout << arr[i];
if (i < r - 1)
cout << ",";
}
}
cout << "]\n";
}
else
{
cout << "error\n";
}
}
return 0;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 14500 : 테트로미노 (0) | 2024.06.14 |
---|---|
[ 백준 / C++ ] 1011 : Fly me to the Alpha Centauri (0) | 2024.06.13 |
[ 백준 / C++ ] 12904 : A와 B (0) | 2024.06.11 |
[ 백준 / C++ ] 14490 : 백대열 (0) | 2024.06.10 |
[ 백준 / C++ ] 15486 : 퇴사 2 (0) | 2024.06.05 |