메모리

시간

2812 KB

112 ms

Code

#include <cstdio>
#include <stack>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string n;
	stack <char> s1;
	stack <char> s2;
	int m, len;

	cin >> n;
	cin >> m;
	len = n.length();
	
	for (int i = 0; i < len; i++)
	{
		s1.push(n[i]);
	}
	
	char input;
	for (int i = 0; i < m; i++)
	{
		cin >> input;
		if (input == 'L' && !s1.empty())
		{
				s2.push(s1.top());
				s1.pop();
		}
		else if (input == 'P')
		{
			cin >> input;
			s1.push(input);
		}
		else if (input == 'D' && !s2.empty())
		{
				s1.push(s2.top());
				s2.pop();
		}
		else if (input == 'B' && !s1.empty())
				s1.pop();
	}
	while (!s1.empty())
	{
		s2.push(s1.top());
		s1.pop();
	}
	while (!s2.empty())
	{
		printf("%c", s2.top());
		s2.pop();
	}
	return 0;
}

문제 풀이

두개의 stack을 사용해 커서의 위치를 구분하였고 입력값에 따라 두개의 스택에서 값을 push, pop하여

모든 입력이 끝나면 순서대로 출력을 위해 s1 → s2 에 담은 뒤 출력하였다.