알고리즘/BOJ

문제 5430번 AC

꾸준함. 2018. 9. 12. 01:01

문제 링크입니다: https://www.acmicpc.net/problem/5430


생각보다 구현하기 까다로운 문제였습니다.

'R' 명령어가 올 때 직접 뒤집지 않고 boolean 변수 left를 통해 해당 덱이 뒤집어져있는지의 여부를 확인합니다.

뒤집어져있을 때 'D' 명령어가 들어오면 pop_back, 뒤집어져있지 않을 때 'D' 명령어가 들어오면 pop_front를 해줍니다.

물론, 덱이 비어있을 때는 "error"를 출력해줍니다.


#include <iostream>

#include <string>

#include <deque>

#include <algorithm>

using namespace std;

 

int main(void)

{

        ios_base::sync_with_stdio(0);

        cin.tie(0);

        int test_case;

        cin >> test_case;

 

        for (int t = 0; t < test_case; t++)

        {

                 string func;

                 cin >> func;

 

                 int N;

                 cin >> N;

 

                 string arr;

                 cin >> arr; //배열 입력

 

                 deque<int> dq; //

 

                 string temp; //두 자릿수 이상 숫자를 위하여

                 for (int i = 0; i < arr.length(); i++)

                         if (arr[i] == '[')

                                 continue;

                         else if ('0' <= arr[i] && arr[i] <= '9')

                                 temp += arr[i];

                         else if (arr[i] == ',' || arr[i] == ']')

                         {

                                 if (!temp.empty())

                                 {

                                          dq.push_back(stoi(temp));

                                          temp.clear();

                                 }

                         }

 

                 bool print = true; //error가 아닐 경우 true

                 bool left = true; //뒤집어져있지 않으면 true

                 for (int i = 0; i < func.length(); i++)

                         if (func[i] == 'R')

                                 left = !left;

                         else

                         {

                                 if (dq.empty())

                                 {

                                          print = false;

                                          cout << "error\n";

                                          break;

                                 }

                                 else

                                          if (left)

                                                  dq.pop_front();

                                          else

                                                  dq.pop_back();

                         }

                

                 if (print)

                 {

                         if (left)

                         {

                                 cout << "[";

                                 while (!dq.empty())

                                 {

                                          cout << dq.front();

                                          dq.pop_front();

                                          if (!dq.empty())

                                                  cout << ",";

                                 }

                                 cout << "]\n";

                         }

                         else

                         {

                                 cout << "[";

                                 while (!dq.empty())

                                 {

                                          cout << dq.back();

                                          dq.pop_back();

                                          if (!dq.empty())

                                                  cout << ",";

                                 }

                                 cout << "]\n";

                         }

                 }

        }

        return 0;

}


개발환경:Visual Studio 2017


지적, 조언, 질문 환영입니다! 댓글 남겨주세요~

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 11772번 POT  (0) 2018.09.14
백준 3988번 수 고르기  (0) 2018.09.12
백준 10866번 덱  (0) 2018.09.12
백준 15386번 Birokracija  (0) 2018.09.11
백준 1188번 음식 평론가  (0) 2018.09.11