‡ CODING TEST STUDY ‡/º 백준

[백준 1406번 C++] 에디터

Trudy | 송연 2023. 9. 9. 23:30

1406번: 에디터 (acmicpc.net)

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net


중간 삽입/삭제가 일어나기 때문에 연결 리스트로 한번 풀어봄

#include <list>
#include <string>
#include <iostream>

using namespace std;

int main() {
    string s;
    int m;
    char op, add;
    list<char> l;
    cin >> s >> m;
    
    for(int i=0; i<s.length(); i++){
        l.push_back(s[i]);
    }
    
    list<char>::iterator cur = l.end();
    
    for(int i=0; i<m; i++){
        cin >> op;
        switch(op){
            case 'L':
                if(cur!=l.begin()) cur--;
                break;
            case 'D':
                if(cur!=l.end())cur++;
                break;
            case 'B':
                if(cur!=l.begin()){
                    cout << "지움 ";
                    l.erase(--cur);
                } 
                break;
            case 'P':
                cin >> add;
                l.insert(cur, add);
                cout << add << "추가함 ";
                break;
        }   
    }
    
    for(list<char>::iterator it=l.begin(); it!=l.end(); it++){
		cout << *it;
	}
    
   return 0;
}

[BOJ 백준] 1406번 에디터 / C++ | Ingyung Blog (iingang.github.io)

 

[BOJ 백준] 1406번 에디터 / C++

Contents

iingang.github.io

근데 3번째 예시가 출력이 잘 안됨

[백준 1406] 에디터 - C++ / 알고리즘 '연결 리스트' (tistory.com)

 

[백준 1406] 에디터 - C++ / 알고리즘 '연결 리스트'

문제 https://www.acmicpc.net/problem/1406 1406번: 에디터 문제 한 줄로 된 간단한 에디터를 구현하려고 한다. 이 편집기는 영어 소문자만을 기록할 수 있는 편집기로, 최대 600,000글자까지 입력할 수 있다.

bunnnybin.tistory.com

이거 참고해서 'B' 에서

cur = l.erase(--cur);

cur = 만 추가해줬더니 해결 됨

(??)

 

#include <list>
#include <string>
#include <iostream>

using namespace std;

int main() {
    string s;
    int m;
    char op, add;
    list<char> l;
    cin >> s >> m;
    
    for(int i=0; i<s.length(); i++){
        l.push_back(s[i]);
    }
    
    list<char>::iterator cur = l.end();
    
    for(int i=0; i<m; i++){
        cin >> op;
        switch(op){
            case 'L':
                if(cur!=l.begin()) cur--;
                break;
            case 'D':
                if(cur!=l.end())cur++;
                break;
            case 'B':
                if(cur!=l.begin()){
                    cur = l.erase(--cur);
                } 
                break;
            case 'P':
                cin >> add;
                l.insert(cur, add);
                break;
        }   
    }
    
    for(list<char>::iterator it=l.begin(); it!=l.end(); it++){
		cout << *it;
	}
    
   return 0;
}