Algorithm/백준

[백준] 10866 덱 with C++

nowkoes 2023. 5. 17. 00:00

문제설명


입출력 예제


개념

 문자열을 입력받아 덱의 기본 동작을 확인하는 간단한 문제다. dequeue의 기본적인 원리를 이해하고 있다면 쉽게 해결할 수 있다.


풀이

#include <iostream>
#include <deque>
using namespace std;

int main()
{
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int N, n;
	cin >> N;
	deque<int> dq;
	string str;
	
	while (N--)
	{
		cin >> str;

		if (str == "push_back")
		{
			cin >> n;
			dq.push_back(n);
		}

		if (str == "push_front")
		{
			cin >> n;
			dq.push_front(n);
		}

		if (str == "front")
		{
			if (dq.empty())
				cout << "-1\n";

			else
				cout << dq.front() << "\n";
		}

		if (str == "back")
		{
			if (dq.empty())
				cout << "-1\n";

			else
				cout << dq.back() << "\n";
		}

		if (str == "size")
			cout << dq.size() << "\n";

		if (str == "empty")
			cout << dq.empty() << "\n";

		if (str == "pop_front")
		{
			if (dq.empty())
				cout << "-1\n";

			else
			{
				n = dq.front();
				cout << n << "\n";
				dq.pop_front();
			}
		}

		if (str == "pop_back")
		{
			if (dq.empty())
				cout << "-1\n";

			else
			{
				n = dq.back();
				cout << n << "\n";
				dq.pop_back();
			}
		}
	}
}

 

반응형

'Algorithm > 백준' 카테고리의 다른 글

[백준] 5430 AC with C++  (0) 2023.05.19
[백준] 1021 회전하는 큐 with C++  (0) 2023.05.18
[백준] 1966 프린터 큐 with C++  (0) 2023.05.16
[백준] 11866 요세푸스 문제 0 with C++  (0) 2023.05.15
[백준] 18258 큐 2 with C++  (2) 2023.05.14