Published 2020. 12. 29. 01:44
 

1406번: 에디터

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

www.acmicpc.net

#include <iostream>
#include <string>
#include <stack>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	string input_str; cin >> input_str;
	stack<char> cursor_left, cursor_right;
	for (char& ch : input_str) { cursor_left.push(ch); }
	int t; cin >> t;
	while (t--) {
		string cmd; cin >> cmd;
		if (cmd == "L" && !cursor_left.empty()) {
		//move cursor to left
			cursor_right.push(cursor_left.top());
			cursor_left.pop();
		}
		else if (cmd == "D" && !cursor_right.empty()) {
		//move cursor to right
			cursor_left.push(cursor_right.top());
			cursor_right.pop();
		}
		else if (cmd == "B" && !cursor_left.empty()) {
		//erase character to left;
			cursor_left.pop();
		}
		else if (cmd == "P") {
		//add character to left
			char ch; cin >> ch;
			cursor_left.push(ch);
		}
	}

	while (!cursor_left.empty()) {
		cursor_right.push(cursor_left.top());
		cursor_left.pop();
	}
	while (!cursor_right.empty()) {
		cout << cursor_right.top();
		cursor_right.pop();
	}
	return 0;
}

'Problem set' 카테고리의 다른 글

[백준] 1676: 팩토리얼 0의 개수  (0) 2020.12.29
[백준] 1463: 1로 만들기  (0) 2020.12.29
[백준] 1373: 2진수 8진수  (0) 2020.12.29
[백준] 1212: 8진수 2진수  (0) 2020.12.29
[백준] 1158: 요세푸스 문제  (0) 2020.12.29
복사했습니다!