알고리즘/문제풀이 :백준

[백준][C++] 5430 - AC

각쿄 2025. 5. 15. 19:11

📌문제 설명

https://www.acmicpc.net/problem/5430

💡생각

#include <iostream>
#include <queue>
#include <sstream>
#include <algorithm>
#include <list>
using namespace std;

void printing(list<int> * l)
{
    cout << "[";
    for (auto it = l->begin(); it != l->end(); ++it) {
        cout << *it;
        if (next(it) != l->end()) cout << ",";
    }
    cout << "]";
    cout << '\n';
}

int main()
{
    int T;
    cin >> T;
    string command;
    int count;
    string arr_str;

    for(int i = 0; i < T;i ++)
    {
        list<int> l;
        queue<char> q;
        bool flag = true;
        cin >> command >> count >> arr_str;
        arr_str = arr_str.substr(1, arr_str.size() - 2);
        stringstream ss(arr_str);
        string token;
        while(getline(ss, token, ',')){
            l.push_back(stoi(token));
        }
        for(char j : command){
            q.push(j);
        }
        while(!q.empty()){
            char temp = q.front();
            q.pop();

            if(temp == 'R'){
                reverse(l.begin(), l.end());
            }else{
                if(l.empty()){
                    cout << "error" << '\n';
                    flag = false;
                    break;
                }else{
                    l.erase(l.begin());
                }
            }
        }
        if(flag) {
            printing(&l);
        }
    }
}

Queue를 이용해서 들어왔던 command를 순차적으로 저장해 하나씩 수행할려고 했다.

Vector에는 들어온 값을 저장해서 벡터연산을 수행할려고 했다.

 

결과는 시간초과 + 정수값을 제대로 저장하지 못하는 문제가 생겨버렸다!!!

R명령어가 나올때 마다 벡터를 뒤집었고 숫자는 0~10까지만 생각해서 저장을 한게 문제였다.

🔥풀이

#include <iostream>
#include <sstream>
#include <algorithm>
#include <list>
using namespace std;

void printing(list<int> * l, bool rev)
{

    cout << "[";
    if(rev){
        for (auto it = l->rbegin(); it != l->rend(); ++it) {
            cout << *it;
            if (next(it) != l->rend()) cout << ",";
        }
    }else{
        for (auto it = l->begin(); it != l->end(); ++it) {
            cout << *it;
            if (next(it) != l->end()) cout << ",";
        }
    }

    cout << "]";
    cout << '\n';
}

int main()
{
    int T;
    cin >> T;
    string command;
    int count;
    string arr_str;

    for(int i = 0; i < T;i ++) {
        list<int> l;
        bool flag = true;
        bool rev = false;
        cin >> command >> count >> arr_str;
        arr_str = arr_str.substr(1, arr_str.size() - 2);
        stringstream ss(arr_str);
        string token;
        while (getline(ss, token, ',')) {
            l.push_back(stoi(token));
        }

        for (char j: command) {
            if (j == 'R') {
                rev = !rev;
            } else {
                if (l.empty()) {
                    cout << "error" << '\n';
                    flag = false;
                    break;
                } else {
                    if(rev){
                        l.pop_back();
                    }else{
                        l.pop_front();
                    }
                }
            }
        }
        if (flag) {
            printing(&l,rev);
        }
    }
}
  • Vector대신 List를 사용해서 문제를 풀었습니다.
  • stringstream으로 입력을 받아서 ,를 기준으로 숫자를 저장해줬다.
  • R명령어가 나올 때 마다 계속 뒤집는게 아니라 bool함수를 이용해 앞에서, 뒤에서 제거연산을 수행하면서 시간복잡도를 개선하였습니다.

이렇게 문제를 해결했다!!