[백준] 1406 - 에디터 C++ 최적화

최적화 시킨 것
 ios::sync_with_stdio(false);
cin.tie(nullptr);
로 입력 최적화를 시켜주었고
P L D B 입력을 모두 if elseif 문으로 하여 조건문을 최적화 시켜주었습니다.
코드
#include <iostream>
#include <list>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string n;
    cin >> n;

    int M;
    cin >> M;

    // 문자열을 리스트에 뒤로 삽입(a <- b <- c <- d)
    list<char> edit;
    for (char c : n) {
        edit.push_back(c);
    }
    auto cursor = edit.end(); // 커서를 리스트의 끝으로 설정

    // M만큼 반복
    for (int i = 0; i < M; i++) {

        // 명령어 입력
        char command;
        cin >> command;

        if (command == 'P') {
            char addC;
            cin >> addC;
            edit.insert(cursor, addC);
        }
        else if (command == 'L' && cursor != edit.begin()) {
            cursor--;
        }
        else if (command == 'D' && cursor != edit.end()) {
            cursor++;
        }
        else if (command == 'B' && cursor != edit.begin()) {
            --cursor;
            cursor = edit.erase(cursor);
        }
    }

    // 결과 출력
    for (char c : edit) {
        cout << c;
    }
    cout << endl;

    return 0;
}
처음 풀었을 때

최적화 후

 

와! 핑거스냅! 136ms->60ms 

'백준' 카테고리의 다른 글

[백준] 1406번 - 에디터 C++  (0) 2024.11.23
[백준] 18258번 - 큐2 C++  (0) 2024.11.18
[백준] 1966번 - 프린터 큐 C++  (4) 2024.11.12
[백준] 1158번 - 요세푸스 문제 C++  (0) 2024.11.11
[백준] 10845번 - 큐 C++  (0) 2024.11.10