반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. Deque 개념

2. Deque STL


덱(혹은 데크)(Deque)의 개념 :: http://www.crocus.co.kr/316

STL로 구현하지 않은 덱 소스 코드 :: http://www.crocus.co.kr/317 


Deque STL을 이용하여 주어진 내용 그대로 따라가면 문제를 해결 할 수 있다.


하지만 STL을 이용하기전에 데크에 대해 한번 직접 짜 보는 경험은 자료구조 연습에 있어 매우 중요할 듯 하다.


소스 코드 : 


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstdio>
#include <deque>
#include <string>
 
using namespace std;
 
int main()
{
    int n;
    deque<int> dq;
 
    cin >> n;
 
 
    while (n--)
    {
        string str;
        int val;
        cin >> str;
 
        // push_back문자를 찾은 경우
        if (str.find("push_back"== 0)
        {
            cin >> val;
            dq.push_back(val);
        }
 
        // push_front문자를 찾은 경우
        else if (str.find("push_front"== 0)
        {
            cin >> val;
            dq.push_front(val);
        }
 
        else if (str.find("pop_back"== 0)
        {
            if (dq.empty() == 1)
                cout << "-1" << endl;
            else
            {
                cout << dq.back() << endl;
                dq.pop_back();
            }
        }
 
        else if (str.find("pop_front"== 0)
        {
            if (dq.empty() == 1)
                cout << "-1" << endl;
            else
            {
                cout << dq.front() << endl;
                dq.pop_front();
            }
        }
 
        else if (str.find("front"== 0)
        {
            if (dq.empty() == 1)
                cout << "-1" << endl;
            else
                cout << dq.front() << endl;
        }
 
        else if (str.find("back"== 0)
        {
            if (dq.empty() == 1)
                cout << "-1" << endl;
            else
                cout << dq.back() << endl;
        }
 
        else if (str.find("size"== 0)
            cout << dq.size() << endl;
 
        else if (str.find("empty"== 0)
            cout << dq.empty() << endl;
    }
}
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1668번] 트로피 진열  (0) 2017.01.08
[1991번] 트리 순회  (0) 2017.01.08
[2217번] 로프  (0) 2016.12.22
[2501번] 약수 구하기  (0) 2016.12.22
[1977번] 완전 제곱수  (0) 2016.12.22