[백준/BOJ] 백준 10866번 : 덱
2020. 8. 28. 18:08ㆍ알고리즘 문제풀이
https://www.acmicpc.net/problem/10866
deque를 사용해 문제를 해결했다.
코드
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
using namespace std;
int n;
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
deque<int> dq; //deque를 사용한다
string command;
int input;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> command;
if (command == "push_front")
{
cin >> input;
dq.push_front(input);
}
else if (command == "push_back")
{
cin >> input;
dq.push_back(input);
}
else if (command == "pop_front")
{
if (dq.empty())
cout << -1 << "\n";
else
{
cout << dq.front() << "\n";
dq.pop_front();
}
}
else if (command == "pop_back")
{
if (dq.empty())
cout << -1 << "\n";
else
{
cout << dq.back() << "\n";
dq.pop_back();
}
}
else if (command == "size")
{
cout << dq.size() << "\n";
}
else if (command == "empty")
{
if (dq.empty())
cout << 1 << "\n";
else
cout << 0 << "\n";
}
else if (command == "front")
{
if (dq.empty())
cout << -1 << "\n";
else
cout << dq.front() << "\n";
}
else if (command == "back")
{
if (dq.empty())
cout << -1 << "\n";
else
cout << dq.back() << "\n";
}
}
return 0;
}
'알고리즘 문제풀이' 카테고리의 다른 글
[백준/BOJ] 백준 5430번 : AC (0) | 2020.08.29 |
---|---|
[백준/BOJ] 백준 1021번 : 회전하는 큐 (0) | 2020.08.28 |
[백준/BOJ] 백준 2164번 : 카드2 (0) | 2020.08.28 |
[백준/BOJ] 백준 18258번 : 큐 2 (0) | 2020.08.28 |
[백준/BOJ] 백준 10845번 : 큐 (0) | 2020.08.28 |