728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <string>
using namespace std;
// [백준 10866 c++ O] 덱
// 문제: 덱의 기본기능 구현
// 접근1: deque STL로 구현
// 풀이:
// n입력
// 명령문 반복 입력 및 출력
// 시간복잡도: O(n)
int n;
deque<int> dq;
string cmd;
int num;
int main()
{
ios::sync_with_stdio(false); // 계산시간 단축 // 문제마다 오류 유무 다름
cin.tie(nullptr); cout.tie(nullptr);// 입출력 시간 단축 // 이것을 쓰면 scanf,printf섞어 쓰면 안됨
cin >> n;
while (n--) {
cin >> cmd;
if (cmd == "push_front") {
cin >> num;
dq.push_front(num);
}
if (cmd == "push_back") {
cin >> num;
dq.push_back(num);
}
if (cmd == "pop_front") {
if (dq.empty()) { cout << -1 << '\n'; continue; }
cout << dq.front() << '\n';
dq.pop_front();
}
if (cmd == "pop_back") {
if (dq.empty()) { cout << -1 << '\n'; continue; }
cout << dq.back() << '\n';
dq.pop_back();
}
if (cmd == "size") {
cout << dq.size() << '\n';
}
if (cmd == "empty") {
cout << dq.empty() << '\n';
}
if (cmd == "front") {
if (dq.empty()) { cout << -1 << '\n'; continue; }
cout << dq.front() << '\n';
}
if (cmd == "back") {
if (dq.empty()) { cout << -1 << '\n'; continue; }
cout << dq.back() << '\n';
}
}
return 0;
}
|
cs |
반응형
'Algorithm_BOJ(백준) > 구현(자료구조)(Data structure)' 카테고리의 다른 글
[백준 9093 c++ VV] 단어 뒤집기 (0) | 2021.08.16 |
---|---|
[백준 1158 c++ O] 요세푸스 문제 (0) | 2021.08.16 |
[백준 10845 c++ O] 큐 (0) | 2021.07.27 |
[백준 10773 c++ O] 제로 (0) | 2021.07.22 |
[백준 9093 c++ V] 단어 뒤집기 (0) | 2021.07.22 |