풀이 보관함

[C++] 백준 1406번: 에디터 본문

problem solving/백준

[C++] 백준 1406번: 에디터

viin 2022. 7. 11. 18:27

문제

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

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net

풀이

최근에 stack문제만 모아서 푸니까 그냥 ktx타고 봐도 스택 문제였다...

배열로 풀다가 인덱스 쫌쫌따리 따라가기 귀찮아서 라이브러리를 이용해서 풀어주었다!

 

소스

#include <iostream>
#include <string>
#include <stack>
 
int main()
{
    std::stack<char> left, right;
    std::string text;
    int M = 0;
    char op;
    
    std::cin >> text >> M;
    
    for(const auto c: text)
    {
        left.push(c);
    }
 
    while(M--)
    {
        std::cin >> op; 
        if (op == 'L')
        {
            if (left.empty()) continue;
            right.push(left.top());
            left.pop();
        }
        else if (op == 'D')
        {
            if (right.empty()) continue;
            left.push(right.top());
            right.pop();
        }
        else if (op == 'B')
        {
            if (left.empty()) continue;
            left.pop();
        }
        else if (op == 'P')
        {
            std::cin >> op;
            left.push(op);
        }
    }
    
    while(!left.empty())
    {
        right.push(left.top());
        left.pop();
    }
    while(!right.empty())
    {
        std::cout << right.top();
        right.pop();
    }
 
    return 0;
}
cs