Study hard
(c++)백준 10866번: 덱 본문
https://www.acmicpc.net/problem/10866
[풀이]
C++ STL의 deque를 사용하여 풀 수 있다.
명령을 입력받는 동시에 deque명령을 실행해준다.
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int N;
string str;
deque<int>d;
void solution() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> str;
if (str == "push_front") {
int pushNum;
cin >> pushNum;
d.push_front(pushNum);
}
else if (str == "push_back") {
int pushNum;
cin >> pushNum;
d.push_back(pushNum);
}
else if (str == "pop_front") {
if (d.empty()) {
cout << -1 << endl;
continue;
}
cout << d.front() << endl;
d.pop_front();
}
else if (str == "pop_back") {
if (d.empty()) {
cout << -1 << endl;
continue;
}
cout << d.back() << endl;
d.pop_back();
}
else if (str == "size") {
cout << d.size() << endl;
}
else if (str == "empty") {
cout << d.empty() << endl;
}
else if (str == "front") {
if (d.empty()) {
cout << -1 << endl;
continue;
}
cout << d.front() << endl;
}
else if (str == "back") {
if (d.empty()) {
cout << -1 << endl;
continue;
}
cout << d.back() << endl;
}
}
}
int main() {
solution();
return 0;
}
'백준 > 여러가지 문제들' 카테고리의 다른 글
(c++)백준 10809번: 알파벳 찾기 (0) | 2020.06.12 |
---|---|
(c++)백준 10808번: 알파벳 개수 (0) | 2020.06.12 |
(c++)백준 10845번: 큐 (0) | 2020.06.12 |
(c++)백준 10799번: 쇠막대기 (0) | 2020.06.12 |
(c++)백준 9012번: 괄호 (0) | 2020.06.12 |