백준 문제는 시간초과나 메모리초과에 신경을 써야하는게 많은 것 같다.
이번 문제도 string과 cursor위치를 int로 체크해주면서 했더니, 시간초과가 떠서 list로 구현.
데이터의 삭제나 추가가 번번히 일어나는 경우는 string 보다는 list가 효과적이라는 사실을 깨달음.
C++에서 list는 많이 안써봐서 낯설다.
내가 짠 코드
#include<iostream>
#include<string>
#include<list>
#include<iterator>
using namespace std;
int main(void){
int N,i;
char op[4]={'L','D','B','P'};
list<char> data;
list<char>::iterator cursor = data.end();
string input,operand;
getline(cin,input);
for(i=0;i<input.length();i++) data.push_back(input[i]);
scanf("%d\n",&N);
for(i=0;i<N;i++){
getline(cin,operand);
if(operand[0] == op[0]) {
if(cursor!=data.begin()) cursor --;
}
else if(operand[0] == op[1]) {
if(cursor!=data.end()) cursor++;
}
else if(operand[0] == op[2]){
if(cursor!=data.begin()){
cursor = data.erase(--cursor);
}
}
else if(operand[0] == op[3]){
data.insert(cursor,operand[2]);
}
}
for(cursor=data.begin();cursor!=data.end();cursor++) cout << *cursor;
cout << endl;
return 0;
}
1406번: 에디터
첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수
www.acmicpc.net
'Problem Solving > 백준' 카테고리의 다른 글
[Kotlin] 코틀린 중복된 횟수 구하기 (0) | 2022.09.30 |
---|---|
[백준] 1966: 프린터큐 (0) | 2021.02.26 |
[백준] 9012: 괄호 (0) | 2021.02.24 |
[백준] 10816: 숫자 카드 2 (0) | 2021.02.24 |
[백준] 11651번 : 좌표 정렬하기 2 (0) | 2021.02.24 |