Little Jay

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

알고리즘/BOJ

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

Jay, Lee 2021. 8. 18. 10:50

스택을 활용하는 전형적인 문제

처음에 커서라는 단어를 봤을 때 이걸 환영 링크드 리스트로 구현을 해야되나라고 생각을 했었는데

생각을 해보니까 커서를 기준으로 왼쪽, 오른쪽으로 스택을 나누어 풀면 쉽겠다라고 생각했다.

 

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

int main() {

	string s;
	cin >> s;
	int n;
	cin >> n;

	string temp;
	stack<char> left;
	stack<char> right;

	for (int i = 0; i < s.length(); i++) {
		left.push(s[i]);
	}

	for (int i = 0; i < n; i++) {
		cin >> temp;
		if (temp == "L") {
			if (left.empty())
				continue;
			else {
				right.push(left.top());
				left.pop();
			}
		}
		else if (temp == "D") {
			if (right.empty())
				continue;
			else {
				left.push(right.top());
				right.pop();
			}
		}
		else if (temp == "B") {
			if (left.empty())
				continue;
			else {
				left.pop();
			}
		}
		else if (temp == "P") {
			char x;
			cin >> x;
			left.push(x);
		}
	}

	while (!left.empty()) {
		right.push(left.top());
		left.pop();
	}
	while (!right.empty()) {
		cout << right.top();
		right.pop();
	}

	return 0;
}

'알고리즘 > BOJ' 카테고리의 다른 글

[C++] 백준 1260번 - DFS와 BFS  (0) 2021.08.19
[C++] 백준 1931번 - 회의실 배정  (0) 2021.08.18
[C++] 백준 4889번 - 안정적인 문자  (0) 2021.08.17
[C++] 백준 7568번 - 덩치  (0) 2021.08.17
[C++] 백준 1764번 - 듣보잡  (0) 2021.08.17
Comments