#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
int n;
string temp;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> temp;
stack<char> left; //커서의 좌측
stack<char> right; //커서의 우측 //우측은 읽는 순서가 반대 pop해야됨
for (int j = 0; j < temp.length(); j++)
{
if (temp[j] == '<')
{
if (!left.empty()) //left가 비어있지 않을때만
{
right.push(left.top()); //왼쪽의 오른쪽끝의 값을 우측에넣는다.
left.pop(); //왼쪽의 우측끝값 삭제
}
}
else if (temp[j] == '>')
{
if (!right.empty())
{
left.push(right.top()); //오른쪽의 첫번째 값을 왼쪽에 넣는다.
right.pop();
}
}
else if (temp[j] == '-')
{
if (!left.empty()) left.pop(); //현재 커서의 왼쪽값 삭제
}
else
{
left.push(temp[j]); //현재 커서에 값 넣기
}
}
string result;
string result2;
while (!right.empty()) {
left.push(right.top());
right.pop();
}
while (!left.empty()) {
result += left.top();
left.pop();
}
for (int j = 0; j < result.length(); j++)
{
result2 += result[result.length() - j - 1]; //반대로 값을 뒤집어준다.
}
cout << result2 <<"\n";
}
return 0;
}
후입 선출을 이용한 커서 위치를 사용하였다.
처음에는 커서위치를 변수로 따로 번호를 두어 사용하려했지만
이게 더 좋은 선택인 것 같다.
'Algorithm > Baekjoon' 카테고리의 다른 글
[C++] 백준 1427 소트인사이드 (0) | 2021.02.03 |
---|---|
[C++] 백준 1920 수 찾기 (0) | 2021.02.02 |
[C++] 백준 1966 프린터 큐 (0) | 2021.02.01 |
[C++] 백준 1874 스택 수열 (0) | 2021.02.01 |
[C++] 백준 2798 블랙잭 (0) | 2021.02.01 |