Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Last active September 4, 2017 13:10
Show Gist options
  • Save fpdjsns/214f6628565f16e59895e817bcee2a06 to your computer and use it in GitHub Desktop.
Save fpdjsns/214f6628565f16e59895e817bcee2a06 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
stack<char> s1; //커서 앞
stack<char> s2; //커서 뒤
//커서가 위치한 부분은 s1의 top 뒷부분
char arr[100000]; //입력한 문자열
int n; //입력할 명령어 수
char edit; //명령
char c; //문자
cin >> arr; //문자열 입력
//s1에 넣기
for (int i = 0; i < strlen(arr); i++)
s1.push(arr[i]);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> edit;
switch (edit)
{
//커서를 왼쪽으로 한 칸 옮김
case 'L':
if (s1.empty())//s1이 비어있다면 -> 커서가 문장의 맨 앞
break; //무시
s2.push(s1.top());//큐의 뒷 부분을 스택에 넣음
s1.pop();
break;
//커서를 오른쪽으로 한 칸 옮김
case 'D':
if (s2.empty())//스택이 비어있다면 -> 커서가 맨 뒤
break; //무시
s1.push(s2.top());//스택의 윗부분을 큐에 넣음
s2.pop();
break;
//커서 왼쪽에 있는 문제 삭제
case 'B':
if (s1.empty())//s1이 비어있다면 -> 커서가 문장의 맨 앞
break; //무시
s1.pop();
break;
//문자를 커서 오른쪽에 추가, 커서는 추가한 문자 오른쪽
case 'P':
cin >> c;
s1.push(c);
break;
}
}
//문자열 출력
while (!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
while (!s2.empty())
{
cout << s2.top();
s2.pop();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment