[BOJ / C++] 5430 : AC

Taegang Yun·2023년 9월 19일
1

[Gold V] AC - 5430

문제 링크

성능 요약

메모리: 3268 KB, 시간: 40 ms

분류

덱, 파싱, 구현, 문자열, 자료 구조

🥴 Hmm...

일단 문자열이 친절하게 숫자만 들어오는게 아니라 [ , ] 이런 애들이 들어오니까 isdigit해서 숫자만 따로 tmp 에 넣게했었다.
아니 근데 그러니까 [42] 같은 경우는 tmp에 [4, 2] 이렇게 들어갔다.
흐음.. 그래서 코드를 좀 짜줬다
숫자를 세기 시작하면 counting = 1로 놓고 세지 않으면 0으로 둔다

세고 있지 않은데 현재는 기호고 다음 번째가 숫자라면 이제 세기 시작할거니까
counting = 1로 놓고 sum에 다음 번째 수를 더해줌

세고 있으면서 다음 번째가 숫자라면
sum = sum * 10 + (arr[j+1] - '0')
(두 자리 수 이상을 의미하니까)

세고 있는데 다음 번째가 숫자가 아니라면
counting = 0 이제 그만 세고
tmp.push_back(sum) 합한거를 tmp에 넣어준다

그리고 R이 들어오면 reverse.. 하게 하고 쭉 짰는데 아니 시간초과가 났다.

흠..

생각해보니가 reverse는 너무 오래걸릴 거 같다. R을 하면 걍 뒤에서 빼면 된다.
bool로 받아서 R을 할 때마다 isNormal 이 바뀌게 해놨다.

흠.. 그래도 시간초과가 16%에서 생긴다

뭐지.. 흠...

이 문제 분류를 보니가 이 있었다. 엇 혹시?
내가 tmp를 vector로 만들어주고 앞에서 뺄때는 tmp.erase(tmp.begin())을 했는데
그냥 tmp를 deque로 만들어주고 tmp.pop_front() 를 하면 되나?

했는데 바로 되버렸다.

에잉

자료구조를 적절히 이용하는 게 좋겠다.

앞뒤에서 둘다 빠지면 deque로!

🖥️ 소스코드

#define fastio ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
using namespace std;

/* Method Initialization */
void input();
void solve();

/* Variable Initialization */
int n, t;
string p;
string arr;
deque<int> tmp;

int main()
{
    fastio;
    solve();
    return 0;
}

void input(){}

void solve() {
    cin >> t;
    for(int i = 0 ; i < t; i++)
    {
        bool check = false;
        cin >> p;
        cin >> n;
        cin >> arr;
        int sum = 0;
        int counting = 0;
        int isNormal = 1;
        if(!tmp.empty()) tmp.clear();




        for(int j =0 ; j < arr.size()-1; j++){
            if(!counting && !isdigit(arr[j]) && isdigit(arr[j + 1])){
                counting = 1;
                sum += (arr[j + 1] - '0');
            }
            else if(counting && isdigit(arr[j + 1])){
                sum = sum * 10 + (arr[j + 1] - '0');
            }
            else if(counting && !isdigit(arr[j + 1])){
                counting = 0;
                tmp.push_back(sum);
                sum = 0;
            }
        }


        for(int j = 0 ; j < p.size(); j++){
            if(p[j] == 'R'){
                if(isNormal) isNormal = 0;
                else isNormal = 1;
            }
            else{
                if(tmp.empty()){check = true;}
                else{
                    if(isNormal)
                        tmp.pop_front();
                    else tmp.pop_back();
                }
            }
        }
        if(check){ cout << "error" << '\n'; continue;}

        if(tmp.empty()){ cout << "[]" << '\n'; continue;}

        if(!check) {
            cout << '[';
            for (int j = 0; j < tmp.size(); j++) {
                if(isNormal){
                    if (j != tmp.size() - 1) {
                        cout << tmp[j] << ',';
                    }
                    else cout << tmp[j];
                }
                else{
                    if(j != tmp.size() - 1){
                        cout << tmp[tmp.size() - j - 1] << ',';
                    }
                    else cout << tmp[tmp.size() - j - 1];
                }
            }
            cout << ']' << '\n';
        }

    }
}
profile
언젠간 전문가가 되겠지

0개의 댓글