- 문제 링크 : boj.kr/5397
- 난이도 : S2
- 태그 : 스택
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) {
string s;
cin >> s;
stack<char> l, r;
for(auto c : s) {
if(c == '<') {
if(l.empty()) continue;
r.push(l.top());
l.pop();
}
else if(c == '>') {
if(r.empty()) continue;
l.push(r.top());
r.pop();
}
else if(c == '-') {
if(!l.empty()) l.pop();
}
else {
l.push(c);
}
}
while(!l.empty()) {
r.push(l.top());
l.pop();
}
while(!r.empty()) {
cout << r.top();
r.pop();
}
cout << "\n";
}
return 0;
}
풀이
커서를 기준으로 왼쪽과 오른쪽을 나누어 스택에 저장한다.
들어오는 명령에 따라 각 스택을 적절이 push 하고 pop 해준다.
728x90
'PS' 카테고리의 다른 글
BOJ 32653 : 흑백 요리사 (0) | 2024.12.08 |
---|---|
BOJ 1431 : 시리얼 번호 (0) | 2024.12.08 |
BOJ 32861 : 순열 복원 (1) | 2024.12.05 |
BOJ 16993 : 연속합과 쿼리 (0) | 2024.12.04 |
BOJ 17410 : 수열과 쿼리 1.5 (0) | 2024.12.03 |