[C++/백준] 1406 에디터
#1406 에디터
https://www.acmicpc.net/problem/1406
📝문제 포인트
- 중간에 문자를 삽입할 수 있어야한다.
->배열로 푸는 건 비효율적임 - stack과 queue를 사용할 경우 두 개씩 사용한다.
- list를 사용할 수도 있다.(중간 삽입이 자유롭기때문)
✍어떻게 풀었나요
앞 뒤로 삽입 삭제가 가능한 deque를 사용했습니다
deque두 개를 두고, 두 개 사이에 커서가 있다고 생각함
✍코드
#include <iostream>
#include <string>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
string str;
cin >> str;
deque<char> q1,q2;
for (int i = 0; i < str.length(); i++) {
q1.push_back(str[i]);
}
int num;
cin >> num;
for (int i = 0; i < num; i++) {
char testCh;
cin >> testCh;
if (testCh == 'P') {
char x;
cin >> x;
q1.push_back(x);
}
else if (testCh == 'L') {
if (!q1.empty()) {
q2.push_front(q1.back());
q1.pop_back();
}
}
else if (testCh == 'B') {
if (!q1.empty()) {
q1.pop_back();
}
}
else { //D
if (!q2.empty()) {
q1.push_back(q2.front());
q2.pop_front();
}
}
}
while (!q1.empty()) {
cout << q1.front();
q1.pop_front();
}
while (!q2.empty()) {
cout << q2.front();
q2.pop_front();
}
}
Author And Source
이 문제에 관하여([C++/백준] 1406 에디터), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@513sojin/C백준-1406-에디터저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)