Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 스택
- 컴퓨터공학과
- 그래프
- 컴공과
- 북리뷰
- 코딩
- c++
- 알고리즘
- 너비우선탐색
- OS
- DP
- cs
- 정석학술정보관
- 코테
- 브루트포스
- bfs
- vector
- 개발
- 백준
- 컴공
- 구현
- Stack
- 정석
- 자료구조
- coding
- 오퍼레이팅시스템
- Computer science
- 오에스
- Operating System
- 문제풀이
Archives
- Today
- Total
Little Jay
[C++] 백준 5430번 - AC 본문
생각보다 너무 오래 고민했던 문제
처음에는 vector 계속 sort하는 방법을 생각했지만 그러기에는 메모리 제한에 걸릴 것이다.
그래서 검색을 하다가 deque로 하는 방법이 있어서 이 방법을 채택했다.
stack과 queue는 일방적으로 뒤, 앞에 있는 원소만 다룰 수 있지만,
deque는 앞, 뒤의 원소를 둘 다 다룰 수 있다는 장점이 있다.
그래서 reverse했을 때 계속해서 vector로 reverse sort 해줄 필요 없이
reverse한 것을 체크해서 앞이나 뒤에서부터 원소를 출력해주면 된다.
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
bool check, err;
string s, numline;
int n;
while (t--) {
deque<int> dq;
check = true;
err = false;
cin >> s >> n >> numline;
int i = 1;
while (numline[i] != '\0') {
int x = 0;
while (numline[i] >= '0' && numline[i] <= '9') {
x *= 10;
x += int(numline[i] - '0');
i++;
}
if (x != 0) {
dq.push_back(x);
}
i++;
}
i = 0;
while (s[i] != '\0') {
if (s[i] == 'R') {
check = !check;
}
else if (s[i] == 'D') {
if (dq.empty()) {
cout << "error" << "\n";
err = true;
break;
}
if (check) {
dq.pop_front();
}
else {
dq.pop_back();
}
}
i++;
}
if (!err) {
cout << "[";
}
while (!dq.empty()) {
if (check) {
auto c = dq.front();
dq.pop_front();
cout << c;
}
else {
auto c = dq.back();
dq.pop_back();
cout << c;
}
if (!dq.empty())
cout << ",";
}
if (!err) {
cout << "]" << "\n";
}
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 6443번 - 애너그램 (0) | 2021.08.25 |
---|---|
[C++] 백준 1747번 - 소수&팰린드롬 (0) | 2021.08.23 |
[C++] 백준 13241번 - 최소공배수 (0) | 2021.08.21 |
[C++] 백준 11508번 - 2 + 1 세일 (0) | 2021.08.21 |
[C++] 백준 2702번 - 초6 수학 (0) | 2021.08.21 |
Comments