ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 10866 덱 [C++]
    알고리즘 2018. 11. 25. 22:11


    문제 링크 : https://www.acmicpc.net/problem/10866


    소스코드


    #include <iostream>
    #include <deque>
    #include <string>
    using namespace std;
    int main() {
        int N;
        deque<int> dq;
        cin >> N;
        cin.ignore();
        for (int i = 0; i < N; ++i) {
            string text;
            getline(cin, text);
            if (text == "front") {
                if (dq.empty()) cout << -1 << "\n";
                else cout << dq.front() << "\n";
            }
            else if (text == "back") {
                if (dq.empty()) cout << -1 << "\n";
                else cout << dq.back() << "\n";
            }
            else if (text[1] == 'u') { // push
                string backup = text;
                if (text[5] == 'f') { //push_front
                    int x = stoi(backup.erase(0, 10));
                    dq.push_front(x);
                }
                else { // push_back
                    int x = stoi(backup.erase(0, 9));
                    dq.push_back(x);
                }
            }
            else if (text[1] == 'o') {
                if (text[4] == 'f') {
                    if (dq.empty()) cout << -1 << "\n";
                    else {
                        cout << dq.front() << "\n";
                        dq.pop_front();
                    }
                }
                else {
                    if (dq.empty()) cout << -1 << "\n";
                    else {
                        cout << dq.back() << "\n";
                        dq.pop_back();
                    }
                }
            }
            else if (text == "size")
                cout << dq.size() << "\n";
            else if (text == "empty") {
                if (dq.empty()) cout << 1 << "\n";
                else cout << 0 << "\n";
            }
        }
    }


    스택은 후입선출 큐는 선입선출이라면,


    덱은 앞뒤로 푸쉬와 팝을 자유롭게 할 수 있는 자료구조입니다.


    C++ STL 덱 사용법 : http://woodforest.tistory.com/80

    '알고리즘' 카테고리의 다른 글

    백준 2747 피보나치 수 [C++]  (0) 2018.11.25
    백준 1021 회전하는 큐 [C++]  (0) 2018.11.25
    백준 1158 조세퍼스 문제 [C++]  (0) 2018.11.25
    백준 1966 프린터 큐 [C++]  (0) 2018.11.25
    백준 10845 큐 [C++]  (0) 2018.11.25

    댓글

© 2018 TISTORY. All rights reserved.