1406번: 에디터

Memo


스택 두개를 써서 문자를 집어넣는다!

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/3f0006df-bdbe-4113-9256-07966f1d20e4/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8b15d1dd-0154-4cd7-ab19-bad26341d2a1/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e3696c63-a5ec-4e84-b9bf-db7049afe8d1/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/786d2b1b-25bf-4316-9fd1-70bf843e6e57/Untitled.png

마지막에 모든 동작이 끝나면 tmp에 str의 남은 모든 문자를 집어넣고 하나씩 출력한다!

Code


제출 날짜

2021/04/08

메모리

2940 KB

시간

44 ms

#include <iostream>
#include <stack>

std::string str;
std::stack<char> res;
std::stack<char> tmp;

int N;

void output()
{
	while(!tmp.empty())
	{
		std::cout << tmp.top();
		tmp.pop();
	}

}

void solution()
{
	char c;

	for (int i = 0; i < N; ++i)
	{
		std::cin >> c;
		if (c == 'P')
		{
			std::cin >> c;
			res.push(c);
		}
		else if (c == 'B' && !res.empty())
		{
			res.pop();
		}
		else if (c == 'L' && !res.empty())
		{
			tmp.push(res.top());
			res.pop();
		}
		else if (c == 'D' && !tmp.empty())
		{
			res.push(tmp.top());
			tmp.pop();
		}
	}
	while (!res.empty())
	{
		tmp.push(res.top());
		res.pop();
	}
}

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

void preset()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);
}

int main()
{
	preset();
	input();
	solution();
	output();
}