코딩테스트(알고리즘)/baekjoon
[baekjoon] 에디터 1406 (c++)
Cafe Mocha
2022. 6. 14. 15:12
1406번: 에디터
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수
www.acmicpc.net
접근 방법
알고리즘 : 연결 리스트
1. 연결 리스트를 활용한 자료구조 문제
// c++ STL을 활용하여 풀이
// li.erase()의 반환값이 다음요소의 이터레이터를 반환한다는 점 주의!
C++
#include <bits/stdc++.h>
using namespace std;
list<char> li;
int main()
{
freopen("input.txt", "r", stdin); //제출 시 삭제
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin>>s;
int len = s.length();
for(char a : s) {
li.push_back(a);
}
int m;
cin>>m;
auto idx = li.end();
for(int i=0;i<m;i++){
char cmd;
cin >>cmd;
switch (cmd)
{
case 'L':
if(idx != li.begin()) idx--;
break;
case 'D':
if(idx != li.end()) idx++;
break;
case 'P':
char tmp;
cin>>tmp;
li.insert(idx,tmp);
break;
default:
if(idx != li.begin()){
idx--;
idx = li.erase(idx);
}
break;
}
}
for(char a:li) cout<<a;
}