Study hard

(c++)백준 10866번: 덱 본문

백준/여러가지 문제들

(c++)백준 10866번: 덱

Nimgnoej 2020. 6. 12. 21:04

https://www.acmicpc.net/problem/10866

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 ��

www.acmicpc.net

[풀이]

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