알고리즘/리스트

[실버3] 백준 1406번 에디터

뇌장하드 2021. 11. 1. 14:51

입력

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수를 나타내는 정수 M(1 ≤ M ≤ 500,000)이 주어진다. 셋째 줄부터 M개의 줄에 걸쳐 입력할 명령어가 순서대로 주어진다. 명령어는 위의 네 가지 중 하나의 형태로만 주어진다.

출력

첫째 줄에 모든 명령어를 수행하고 난 후 편집기에 입력되어 있는 문자열을 출력한다.

 

#include<bits/stdc++.h>

using namespace std;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	string str;
	cin >> str;
	list<char> L;
	for (auto c : str) L.push_back(c);
	auto cursor = L.end();
	int q;
	cin >> q;
	while (q--) {
		char op;
		cin >> op;
		if (op == 'P') {
			char add;
			cin >> add;
			L.insert(cursor, add);
		}
		else if (op == 'L') {
			if (cursor != L.begin()) {
				cursor--;
			}
			}
			else if (op == 'D') {
				if (cursor != L.end()) cursor++;
			}
			else {
				if (cursor != L.begin()) {
					cursor--;
					cursor = L.erase(cursor);
				}
			}
		}
	for (auto c : L) cout << c;
}

 

https://www.acmicpc.net/problem/1406

 

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

[실버3] 백준 5397번 키로거  (0) 2021.11.01