๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1406๋ฒˆ - ์—๋””ํ„ฐ

by dkswnkk 2021. 11. 6.
 

1406๋ฒˆ: ์—๋””ํ„ฐ

์ฒซ์งธ ์ค„์—๋Š” ์ดˆ๊ธฐ์— ํŽธ์ง‘๊ธฐ์— ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๋ฌธ์ž์—ด์€ ๊ธธ์ด๊ฐ€ N์ด๊ณ , ์˜์–ด ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 100,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ž…๋ ฅํ•  ๋ช…๋ น์–ด์˜ ๊ฐœ์ˆ˜

www.acmicpc.net

#include <iostream>
#include <stack>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    string s; cin >> s;
    int t; cin >> t;

    stack<char>left, right;

    for (int i = 0; i < s.length(); i++) {
        left.push(s[i]);
    }

    while (t--) {

        char com; cin >> com;

        if (com == 'L') {
            if (left.empty()) continue;
            right.push(left.top());
            left.pop();
        }
        else if (com == 'D') {
            if (right.empty()) continue;
            left.push(right.top());
            right.pop();
        }
        else if (com == 'B') {
            if (!left.empty()) left.pop();
        }
        else if (com == 'P') {
            char c; cin >> c;
            left.push(c);
        }

    }
    while (!left.empty()) {
        right.push(left.top());
        left.pop();
    }

    while (!right.empty()) {
        cout << right.top();
        right.pop();
    }

}

๋Œ“๊ธ€