https://www.acmicpc.net/problem/10866
아이디어:
1. STL deque의 사용법을 묻는 기본 문제
2. 덱이 비어있을 때의 예외처리
3. push 입력받고 int tmp; cin >> tmp; 처리하기
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, tmp;
deque<int> DQ;
string str;
cin >> n;
while (n--) {
cin >> str;
if (str == "push_front") {
cin >> tmp;
DQ.push_front(tmp);
}
else if (str == "push_back") {
cin >> tmp;
DQ.push_back(tmp);
}
else if (str == "pop_front") {
if (!DQ.empty()) {
cout << DQ.front() << "\n";
DQ.pop_front();
}
else cout << -1 << "\n";
}
else if (str == "pop_back") {
if (!DQ.empty()) {
cout << DQ.back() << "\n";
DQ.pop_back();
}
else cout << -1 << "\n";
}
else if (str == "size") {
cout << DQ.size() << "\n";
}
else if (str == "empty") {
cout << DQ.empty() << "\n";
}
else if (str == "front") {
if (!DQ.empty()) {
cout << DQ.front() << "\n";
}
else cout << -1 << "\n";
}
else if (str == "back") {
if (!DQ.empty()) {
cout << DQ.back() << "\n";
}
else cout << -1 << "\n";
}
}
return 0;
}
'algorithm' 카테고리의 다른 글
백준 5430번 AC (0) | 2025.03.30 |
---|---|
백준 1021번 회전하는 큐 (0) | 2025.03.30 |
덱 (0) | 2025.03.30 |
백준 2164번 카드2 (0) | 2025.03.29 |
백준 10845번 큐 (0) | 2025.03.29 |