728x90
덱 문제입니다.
숫자를 덱에 입력받고 함수를 실행 했을 때의 결과값을 출력해야합니다.
R = 숫자들 뒤집기 ex) 1234 -> 4321
D = 앞의 문자 제거 ex) 1234 -> 234
deque가 비어있는데 D함수 실행시 -> "error" 출력
덱에 모든 값을 다 넣고
R을 입력받으면 reverse = !reverse를 해주어서
reverse가 true면 D함수에 대해 pop_back()
reverse가 false면 D함수에 대해 pop_front()를 해주었습니다.
비어있는데 D함수 실행시 error를 true로 해주었습니다.
reverse가 true면 d.back()을 이용해 뒤에서 부터 출력하고
reverse가 false면 d.front()를 이용해 앞에서 부터 출력했습니다.
#include <iostream>
#include <algorithm>
#include <deque>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
int ts;
int n;
int num;
string s;
string arr;
bool reverse;
bool error;
deque<int> d;
cin >> ts;
while (ts--) {
d.clear();
num = 0;
reverse = false;
error = false;
cin >> s;
cin >> n;
cin >> arr;
arr = arr.substr(1, arr.size() - 2);
for (int i = 0; i < arr.size(); ++i) {
if (atoi(arr.substr(i, 1).c_str()) != 0 || arr.substr(i, 1).compare("0") == 0) {
num *= 10;
num += stoi(arr.substr(i, 1));
}
else {
d.push_back(num);
num = 0;
}
}
if (num != 0) {
d.push_back(num);
}
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'R') {
reverse = !reverse;
}
else if (s[i] == 'D') {
if (d.empty()) {
error = true;
break;
}
if (reverse == true) {
d.pop_back();
}
else {
d.pop_front();
}
}
}
if (error) {
cout << "error" << endl;
continue;
}
cout << "[";
if (!reverse) {
while (d.size()) {
cout << d.front();
d.pop_front();
if (d.size()) {
cout << ",";
}
}
}
else {
while (d.size()){
cout << d.back();
d.pop_back();
if (d.size()) {
cout << ",";
}
}
}
cout << "]" << endl;
}
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 1992번 쿼드트리 (0) | 2019.09.08 |
---|---|
[C++] 백준 2630번 색종이 만들기 (0) | 2019.09.08 |
[C++] 백준 1021번 회전하는 큐 (0) | 2019.09.08 |
[C++] 백준 10866번 덱 (0) | 2019.09.08 |
[C++] 백준 1966번 프린터 큐 (0) | 2019.09.08 |