백준 풀이 - 덱 연습문제
참고 링크
https://www.acmicpc.net/board/view/25456
문제리뷰
5430 AC : 처음에 배열을 뒤집을 때, 배열을 직접 뒤집어서 시간초과가 남. (reverse 사용)
이후 참고 링크에서 배열을 직접 뒤집으면 안된다고 해서 reverse변수로 상태 확인 후 출력하도록 함.
(head, tail을 통해 출력)
모범 코드에서는 reverse 변수를 확인해서 reverse가 true이면 reverse 함수를 사용해서 직접 뒤집은 후, 출력
기억할 것
1. reverse 함수
2. c++에서 split하는 코드
5430 AC
#include <bits/stdc++.h>
using namespace std;
int n;
string func;
int len;
string arr;
deque<string> DQ;
deque<string> split(string str, char delimiter) {
int previous = 0, current = 0;
deque<string> x;
x.clear();
current = str.find(delimiter);
while(current != string::npos) {
string substring = str.substr(previous, current-previous);
x.push_back(substring);
previous = current + 1;
current = str.find(delimiter, previous);
}
x.push_back(str.substr(previous, current-previous));
return x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int head, tail;
for(int i = 0; i < n; i++) {
bool flag = true;
bool reverse = false;
DQ.clear();
cin >> func >> len >> arr;
if(arr.length() > 2) {
DQ = split(arr.substr(1, arr.length()-2), ',');
}
for(int j = 0; j < func.length(); j++) {
if(func[j] == 'R') {
reverse = (reverse) ? false : true;
}
else if(func[j] == 'D') {
if(DQ.empty()) {
flag = false;
continue;
}
if(reverse) {
DQ.pop_back();
}
else {
DQ.pop_front();
}
}
}
head = 0; tail = DQ.size()-1;
if(flag) {
cout << '[';
if(reverse) {
for(int j = DQ.size()-1; j >= 0; j--) {
cout << DQ[j];
if(j != 0) {
cout << ',';
}
}
}
else {
for(int j = 0; j < DQ.size(); j++) {
cout << DQ[j];
if(j != DQ.size()-1) {
cout << ',';
}
}
}
cout << "]\n";
} else {
cout << "error\n";
}
}
}
'Problem Solving > 문제풀이' 카테고리의 다른 글
기타:: 230115 일기 (0) | 2023.01.15 |
---|---|
기타: : 230111 일기 (0) | 2023.01.11 |
기타:: 230105 일기 (0) | 2023.01.05 |
기타:: 1104 일기 (0) | 2022.11.04 |
기타:: 1101 일기 (0) | 2022.11.02 |