Gold V
https://www.acmicpc.net/problem/5430
선영이는 주말에 할 일이 없어서 새로운 언어 AC를 만들었다. AC는 정수 배열에 연산을 하기 위해 만든 언어이다. 이 언어에는 두 가지 함수 R(뒤집기)과 D(버리기)가 있다.
함수 R은 배열에 있는 수의 순서를 뒤집는 함수이고, D는 첫 번째 수를 버리는 함수이다. 배열이 비어있는데 D를 사용한 경우에는 에러가 발생한다.
함수는 조합해서 한 번에 사용할 수 있다. 예를 들어, "AB"는 A를 수행한 다음에 바로 이어서 B를 수행하는 함수이다. 예를 들어, "RDD"는 배열을 뒤집은 다음 처음 두 수를 버리는 함수이다.
배열의 초기값과 수행할 함수가 주어졌을 때, 최종 결과를 구하는 프로그램을 작성하시오.
input:
- 첫째 줄에 테스트 케이스의 개수 T가 주어진다. T는 최대 100이다.
- 각 테스트 케이스의 첫째 줄에는 수행할 함수 p가 주어진다. p의 길이는 1보다 크거나 같고, 100,000보다 작거나 같다.
- 다음 줄에는 배열에 들어있는 수의 개수 n이 주어진다. (0 ≤ n ≤ 100,000)
- 다음 줄에는 [x1,...,xn]과 같은 형태로 배열에 들어있는 정수가 주어진다. (1 ≤ xi ≤ 100)
- 전체 테스트 케이스에 주어지는 p의 길이의 합과 n의 합은 70만을 넘지 않는다.
output:
- 각 테스트 케이스에 대해서, 입력으로 주어진 정수 배열에 함수를 수행한 결과를 출력한다.
- 만약, 에러가 발생한 경우에는 error를 출력한다.
Idea :
- 순서를 뒤집거나, 가장 앞에 있는 숫자 꺼내기
== 정방향이라면 첫 번째 숫자를, 역방향이라면 마지막 숫자를 지움 - 정방향인지 역방향인지 알아야됨
- 가장 앞에 있는 숫자와 가장 뒤에 있는 숫자에 접근 가능해야 함
- deque
- vector - 남아있는 숫자 배열의 시작 위치와 끝 위치를 가리키는 두 개의 포인터를 이용
Code 1: deque
#include <iostream>
#include <deque>
using namespace std;
#define FORWARD 1
#define REVERSE -1
struct AC {
deque<string> dq;
int reverse_flag;
void init() {
int n;
string input, tmp = "";
cin >> n >> input;
dq.clear();
if (n == 0) return;
for (auto e : input) {
if ('0' <= e && e <= '9') dq.back() += e;
else if (e == '[' || e == ',') dq.push_back("");
}
reverse_flag = FORWARD;
}
bool cmd_R() {
reverse_flag = -reverse_flag;
return false;
}
bool cmd_D() {
if (dq.empty()) return true;
(reverse_flag == FORWARD) ? dq.pop_front() : dq.pop_back();
return false;
}
void print_answer(bool isError) {
if (isError) {
cout << "error\n"; return;
}
cout << "[";
if (!dq.empty()){
if (reverse_flag == FORWARD) {
while(dq.size() > 1) {
cout << dq.front() << ","; dq.pop_front();
}
cout << dq.front();
}
else {
while(dq.size() > 1) {
cout << dq.back() << ","; dq.pop_back();
}
cout << dq.back();
}
}
cout << "]\n";
}
};
void solve(AC &ac) {
string cmds; cin >> cmds;
bool isError = false;
ac.init();
for (auto cmd : cmds) {
switch(cmd) {
case 'R':
ac.cmd_R(); break;
case 'D':
isError = ac.cmd_D(); break;
}
if (isError) break;
}
ac.print_answer(isError);
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// freopen("input.txt", "rt", stdin);
int TC; cin >> TC;
AC ac;
while (TC--) {
solve(ac);
}
}
Code 2: vector
#include <iostream>
#include <vector>
using namespace std;
#define FORWARD 1
#define REVERSE -1
struct AC {
vector<string> nums;
int begin, end;
int sign;
void init() {
int n;
string input;
cin >> n >> input;
nums.clear();
begin = 0; end = n-1;
sign = FORWARD;
if (n == 0) return;
for (auto e : input) {
if ('0' <= e && e <= '9') nums.back() += e;
else if (e == '[' || e == ',') nums.push_back("");
}
}
bool cmd_R() {
sign = -sign;
return false;
}
bool cmd_D() {
if (begin > end) return true;
(sign == FORWARD) ? begin++ : end--;
return false;
}
void print_answer(bool isError) {
if (isError) {
cout << "error\n"; return;
}
cout << "[";
if (begin <= end){
if (sign == REVERSE) {
int tmp = begin; begin = end; end = tmp;
}
for (auto it = begin; it != end; it += sign)
cout << nums[it] << ",";
cout << nums[end];
}
cout << "]\n";
}
};
void solve(AC &ac) {
string p; cin >> p;
bool isError = false;
ac.init();
for (auto op : p) {
switch(op) {
case 'R':
ac.cmd_R(); break;
case 'D':
isError = ac.cmd_D(); break;
}
if (isError) break;
}
ac.print_answer(isError);
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// freopen("input.txt", "rt", stdin);
int TC; cin >> TC;
AC ac;
while (TC--) {
solve(ac);
}
}
Result :
Code 1: deque) 메모리: 6004 KB, 시간: 40 ms
Code 2: vector) 메모리: 8772 KB, 시간: 36 ms
Review :
'코딩테스트 연습' 카테고리의 다른 글
[BOJ] 2110. 공유기 설치 (binary_search, parametric_search) (0) | 2023.01.23 |
---|---|
[BOJ] 1495. 기타리스트 (dp) (0) | 2023.01.23 |
[BOJ] 14502. 연구소 (bfs, bruteforcing, graphs, graph_traversal, implementation) (1) | 2023.01.14 |
[BOJ] 10799. 쇠막대기 (data_structures, stack) (0) | 2023.01.10 |
[BOJ] 9020. 골드바흐의 추측 (math, number_theory, primality_test, 에라토스테네스의 체) (0) | 2023.01.10 |