작심 24/7

[백준] 1406번 에디터 본문

백준

[백준] 1406번 에디터

모닝수박 2020. 6. 6. 18:51
 

1406번: 에디터

문제 한 줄로 된 간단한 에디터를 구현하려고 한다. 이 편집기는 영어 소문자만을 기록할 수 있는 편집기로, 최대 600,000글자까지 입력할 수 있다. 이 편집기에는 '커서'라는 것이 있는데, 커서는

www.acmicpc.net

5397번 키로거 문제와 같다.

커서를 기준으로 스택을 두 개 만든 뒤

 

L -> res가 비어있지 않으면 res의 top을 temp에 push

D -> temp가 비어있지 않으면 temp의 top을 res에 push

B -> res가 비어있지 않으면 pop

P 문자 -> res에 문자 push

 

명령어에 따라 이렇게 구현해주면 된다.

#include <iostream>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string N;
	int M;
	cin >> N >> M;

	stack <char> res, temp;
	for (int i = 0; i < N.size(); i++) res.push(N[i]);

	string order;
	char ch;
	for (int m = 0; m < M; m++) {
		cin >> order;

		if (order == "P") {
			cin >> ch;
			res.push(ch);
		}
		else if (order == "L") {
			if (!res.empty()) {
				temp.push(res.top());
				res.pop();
			}
		}
		else if (order == "D") {
			if (!temp.empty()) {
				res.push(temp.top());
				temp.pop();
			}
		}
		else {
			if (!res.empty()) res.pop();
		}
	}

	while (!temp.empty()) {
		res.push(temp.top());
		temp.pop();
	}
	string result;
	while (!res.empty()) {
		result += res.top();
		res.pop();
	}
	reverse(result.begin(), result.end());

	cout << result;

	return 0;
}

'백준' 카테고리의 다른 글

[백준] 2309번 일곱 난쟁이  (0) 2020.06.07
[백준] 1912번 연속합  (0) 2020.06.07
[백준] 5076번 Web Pages  (0) 2020.06.05
[백준] 2841번 외계인의 기타 연주  (0) 2020.06.05
[백준] 2304번 창고 다각형  (4) 2020.06.05
Comments