티스토리 뷰
https://www.acmicpc.net/problem/5430
생각보다 쉽게 풀려서 다행인 문제였습니다. 단순하게 덱을 어떻게 쓸 줄 안다면 쉽게 푸실 수 있는 문제인 것 같습니다. 여기서 여건은 문자열 파싱을 어떻게 하느냐가 제일 중요했던 것 같습니다. 파싱 함수는 인터넷에 돌아다니는 함수를 긁어서 사용했습니다.
#include <iostream>
#include <queue>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
vector<string> split(string str, char delimiter);
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
int N, M, C;
int input;
string s, num;
vector<string> number;
cin >> C;
for (int i = 0; i < C; i++) {
deque<int> t1;
bool error = false;
cin >> s;
cin >> N;
cin >> num;
num.erase(num.begin());
num.erase(num.end()-1);
number = split(num, ',');
for (string s : number) {
if (atoi(s.c_str())) {
t1.push_back(atoi(s.c_str()));
}
}
bool check = true;
for (char c : s) {
if (c == 'R') {
if (check)
check = false;
else
check = true;
}
else {
if (t1.empty()) {
error = true;
break;
}
if (check)
t1.pop_front();
else
t1.pop_back();
}
}
if(error)
cout << "error";
else if (check) {
cout << "[";
for (int i = 0; i < t1.size(); i++) {
if (i == t1.size()-1)
cout << t1[i];
else
cout << t1[i] << ",";
}
cout << "]";
}
else {
cout << "[";
for (int i = t1.size() - 1; i >= 0; i--) {
if (i == 0)
cout << t1[i];
else
cout << t1[i] << ",";
}
cout << "]";
}
cout << "\n";
}
}
vector<string> split(string input, char delimiter) {
vector<string> answer;
stringstream ss(input);
string temp;
while (getline(ss, temp, delimiter)) {
answer.push_back(temp);
}
return answer;
}
드디어 골드 5 왔네요.. 더 노력해서 플레까지 가보겠습니다.
'자료구조 및 알고리즘 > 문제풀이' 카테고리의 다른 글
백준 C++ 2805번) 나무 자르기 (0) | 2022.08.26 |
---|---|
백준 C++ 1654번) 랜선 자르기 (0) | 2022.08.25 |
백준 C++ 1021번) 회전하는 큐 (0) | 2022.08.24 |
백준 C++ 11866) 오세푸스 문제 0 (0) | 2022.08.23 |
백준 C++2164번) 카드2 (0) | 2022.08.23 |
댓글