| DONE_DATE | 2025/02/01 |
|---|
- ๊ณจ๋ 5
https://www.acmicpc.net/problem/5430
- ๊ตฌํ
- ์๋ฃ ๊ตฌ์กฐ
- ๋ฌธ์์ด
- ํ์ฑ
- ๋ฑ
- ์ด๋ ต์ง ์๋ค. ๊ตฌํ ๋ฌธ์ ๋ต๋ค.
- ๋ฐฐ์ด ํํ์ ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ง๋๋ ๊ฒ ์ด๋ ค์ ๋ค. substr์ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ํด๊ฒฐํ ์ ์๋ค.
arrayString.substr(1, arrayString.size() - 2);๋งจ ์๋ฌธ์์ด๊ณผ ๋งจ ๋ค ๋ฌธ์์ด์ ๋ ๋ฆฐ๋ค. - ํน์
s.erase(s.begin()); s.pop_back();์ผ๋ก๋ ๊ฐ๋ฅํ๋ค.
istringstream iss(arrayString);
string temp;
while (getline(iss, temp, ',')) {
dq.push_back(stoi(temp));
}- ์ด๊ฑด , ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด์ ๋๋๋ ๋ฐฉ๋ฒ์ด๋ค.
getline(iss, temp, ',')๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค. - ๊ทธ๋ฆฌ๊ณ ๋ช ๋ น์ด๋ฅผ ํด์ํ๋ ๋ถ๋ถ์ ๊ฐ๋จํ๋ค. D๋ฉด pop_front, R์ด๋ฉด ๋ค์ง๋๋ค.
- ๋ค์ง๋ ๊ฒ๋ ์ค์ ๋ก ๋ค์ง์ง ์๊ณ ๋ฐฉํฅ์ ๋ฐ๊พธ๋ ๊ฒ์ผ๋ก ํด๊ฒฐํ๋ค.
isForward = !isForward; - isForward์ ๋ฐ๋ผ์ pop_front, pop_back์ ํด์ฃผ๋ฉด ๋๋ค.
- ์ถ๋ ฅํ ๋๋ isForward์ ๋ฐ๋ผ ์ถ๋ ฅ์ ๋ค๋ฅด๊ฒ ํด์ฃผ๋ฉด ๋๋ค.
for(auto i : dq) {
if(!first) {
cout << ",";
}
cout << i;
first = false;
}- ์ฒ์์๋ ๊ทธ๋ฅ ์ถ๋ ฅํ ํ ๋ฐฑ์คํ์ด์ค(\b) ๋ฅผ ํตํด์ ์ง์ฐ๋ ๊ฑธ๋ก ํ๋๋ฐ ๊ทธ๋ฌ๋ฉด
1 D 1 [42] - ๊ฐ์ ์ํฉ์์ ๋ฌธ์ ๊ฐ ์๊ธด๋ค. first๋ฅผ ์ฌ์ฉํด์ ์ถ๋ ฅ์ ์ ์ดํ์.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
string cmd;
int n;
string arrayString;
deque<int> dq;
while (T--) {
cin >> cmd;
cin >> n;
cin >> arrayString;
bool isForward = true; // right or left
bool errorFlag = false;
arrayString = arrayString.substr(1, arrayString.size() - 2);
istringstream iss(arrayString);
string temp;
while (getline(iss, temp, ',')) {
dq.push_back(stoi(temp));
}
for (auto c: cmd) {
if (c == 'D') {
if (dq.empty()) {
cout << "error" << '\n';
errorFlag = true;
break;
}
if (isForward) {
dq.pop_front();
} else {
dq.pop_back();
}
} else { // c == 'R'
isForward = !isForward;
}
}
if (!errorFlag) {
cout << "[";
bool first = true;
if(isForward) {
for(auto i : dq) {
if(!first) {
cout << ",";
}
cout << i;
first = false;
}
} else {
reverse(dq.begin(), dq.end());
for(auto i : dq) {
if(!first) {
cout << ",";
}
cout << i;
first = false;
}
}
cout << "]\n";
}
dq.clear();
}
}