문제
1406번: 에디터
문제 한 줄로 된 간단한 에디터를 구현하려고 한다. 이 편집기는 영어 소문자만을 기록할 수 있는 편집기로, 최대 600,000글자까지 입력할 수 있다. 이 편집기에는 '커서'라는 것이 있는데, 커서는 문장의 맨 앞(첫 번째 문자의 왼쪽), 문장의 맨 뒤(마지막 문자의 오른쪽), 또는 문장 중간 임의의 곳(모든 연속된 두 문자 사이)에 위치할 수 있다. 즉 길이가 L인 문자열이 현재 편집기에 입력되어 있으면, 커서가 위치할 수 있는 곳은 L+1가지 경우가
www.acmicpc.net
내 코드(C++)
#include <iostream>
#include <stack>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
stack<char> left, right;
string s;
cin >> s;
int len = s.length();
/*문자열 입력*/
for (int i = 0; i < len; i++) {
left.push(s[i]);
}
int N;
cin >> N; /*입력할 명령어의 개수*/
while (N--) {
char command;
cin >> command;
if (command == 'L' && !left.empty()) {
right.push(left.top());
left.pop();
}
else if (command == 'D' && !right.empty()) {
left.push(right.top());
right.pop();
}
else if (command == 'B' && !left.empty()) {
left.pop();
}
else if (command == 'P') {
char $;
cin >> $;
left.push($);
}
}
while (!left.empty()) {
right.push(left.top());
left.pop();
}
while (!right.empty()) {
printf("%c", right.top());
right.pop();
}
}
반응형