2016년 4월 20일 수요일

5430 - AC

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

배열이 뒤집혀도 배열의 첫번째 숫자를 버립니다. 다시 말하면, 배열의 마지막 숫자를 버리는 것은 배열이 뒤집혔을때 버리는 것과 같다.

뒤집기 연산이 들어올때마다 "앞에서 버릴 지, 뒤에서 버릴 지"를 번갈아주도록 하면 된다.

pop_front 와 pop_back 을 위해 데크를 사용한다.

같이 보기


#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
using namespace std;

int main(){
    int T;
    scanf("%d ", &T);
    while(T--){
        string cmd, s;
        int i, n, k, num=0;
        cin >> cmd >> n >> s;
        deque<int> deq;
        bool popFront = true;
        for(k=0,i=1; k<n && i<s.size(); ++i){
            if(s[i] >= '0' && s[i] <= '9'){
                num = num*10 + (s[i]-'0');
            } else {
                deq.push_back(num);
                num = 0;
                k += 1;
            }
        }
        
        bool hasError = false;
        for(i=0; i<cmd.size(); ++i){
            if(cmd[i] == 'R') popFront = !popFront;
            else {
                if(deq.empty()){
                    hasError = true;
                    break;
                }
                else if(popFront) deq.pop_front();
                else deq.pop_back();
            }
        }
        if(hasError) puts("error");
        else {
            int len = deq.size();
            if( popFront ){
                printf("[");
                for(i=0; i<len; ++i){
                    printf("%d", deq[i]);
                    if(i!=len-1) printf(",");
                }
                printf("]");
            }
            else {
                printf("[");
                for(i=len-1; i>=0; --i){
                    printf("%d", deq[i]);
                    if(i!=0) printf(",");
                }
                printf("]");
            }
            puts("");
        }
    }
    
    return 0;
}

댓글 없음:

게시글 목록