-
백준 5430번: AC (C++)알고리즘/BOJ 2021. 6. 26. 13:53
문제 : https://www.acmicpc.net/problem/5430
5430번: AC
각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다. 만약, 에러가 발생한 경우에는 error를 출력한다.
www.acmicpc.net
문제는 굉장히 쉽다. 굉장히 쉬운 문제지만 굉장히 귀찮고 짜증나는 문제다.
이 문제의 핵심은 첫번째는 reverse order에서 소비되는 시간을 줄이기 위해 디큐(데크)를 사용하는것과
[1,2,3] 처럼 괴상하게 input으로 들어오는값을 잘 처리하는 것이다.
처음에는 input의 각 숫자를 stoi 함수를 이용해서 처리하니까 시간초과가 나서 char타입을 처리해서 int 타입으로 바꿔주는 방법을 사용했다.
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <map> #include <queue> #include <unordered_map> #include <deque> #include <string> typedef long long ll; using namespace std; deque<int> dq; int main(int argc, char** argv) { cin.tie(NULL); ios_base::sync_with_stdio(false); int tn; cin >> tn; while(tn--){ string func; int n; string l; dq.clear(); bool reverse = false; cin >> func; cin >> n; cin >> l; int idx = 0; while(idx < l.length()){ if(l[idx] == ',' || l[idx] == '[' || l[idx] == ']'){ idx++; continue; } int num = 0; while(l[idx] >= '0' && l[idx] <= '9'){ num = 10*num; num += l[idx] - '0'; idx++; } dq.push_back(num); } int err = 0; for(char c : func){ if(c=='R'){ reverse = !reverse; } else{ if(dq.empty()){ cout << "error\n"; err = 1; break; } if(!reverse){ dq.pop_front(); } else{ dq.pop_back(); } } } if(err==1) continue; cout << '['; while (!dq.empty()) { if (!reverse) { cout << dq.front(); dq.pop_front(); } else { cout << dq.back(); dq.pop_back(); } if (!dq.empty()) { cout << ","; } } cout << "]\n"; } }
'알고리즘 > BOJ' 카테고리의 다른 글
백준 2357번 : 최솟값과 최댓값 (0) 2021.06.28 백준 16562번: 친구비 (0) 2021.06.27 백준 11003번 : 최솟값 찾기 (0) 2021.06.26 백준 1937번: 욕심쟁이 판다 (C++) (0) 2021.06.25 백준 4195번: 친구 네트워크 (C++) (0) 2021.06.25