πŸ‹ 문제링크


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

🍎 μ½”λ“œ 제좜 기둝 (λ©”λͺ¨λ¦¬ 및 μ‹œκ°„)


λ©”λͺ¨λ¦¬ : 2808 KB

μ‹œκ°„ : 40 ms

πŸ‰ Code


#include <iostream>
#include <stack>

int N;
std::stack <char> left;
std::stack <char> right; 
std::stack <char> tmp;

void input_faster()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
    std::string str;
    std::cin >> str;
    for(int i = 0; i < str.size() ; i++){
        left.push(str[i]);
    }
    std::cin >> N;
}

void solve()
{
    char func;
    while (N--){
        std::cin >> func;
        if (func == 'P'){
            char x;
            std::cin >> x;
            left.push(x);
        }
        else if (func == 'L'){
            if (!left.empty()){
                right.push(left.top());
                left.pop();
            }
        }
        else if (func == 'B'){
            if (!left.empty())
                left.pop();
        }
        else if (func == 'D'){
            if (!right.empty()){
                left.push(right.top());
                right.pop();
            }
        }
    }
}

void print_val()
{
   while (!left.empty()){
       tmp.push(left.top());
       left.pop();
   }
   while (!tmp.empty()){
       std::cout << tmp.top();
       tmp.pop();
   }
   while (!right.empty()){
       std::cout << right.top();
       right.pop();
   }
}

int main()
{
	input_faster();
	input();
	solve();
	print_val();
	return (0);
}

πŸ₯ λ©”λͺ¨


λ¬Έμžμ—΄μ—μ„œ μ»€μ„œλ₯Ό μ΄λ™ν•˜κ³  쀑간에 문자λ₯Ό μ‚½μž…ν•˜λŠ” 과정을 μ‰½κ²Œ λ§Œλ“€κΈ° μœ„ν•΄ stack λ‘κ°œλ₯Ό μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

νŽΈμ˜μƒ 두 μŠ€νƒμ˜ 이름을 left, right 라고 ν•˜κ³ 

이해λ₯Ό μœ„ν•΄ right μŠ€νƒμ˜ 쒌우λ₯Ό λ°˜μ „ν•˜μ—¬ κ·Έλ¦¬κ² μŠ΅λ‹ˆλ‹€!

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/414df067-ded0-4252-ba4a-6ca4f0a3a9e7/Untitled.png

각각의 연산을 그림으둜 ν‘œν˜„ν•˜λ©΄ μ•„λž˜μ™€ κ°™κ³