์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- ๋ฐฑํธ๋ํน
- c++
- Reversing
- error
- thymeleaf
- ๋งต
- dfs
- ๋์ ํฉ
- CVE
- web
- ์ด๋ถ ํ์
- ์๋ฎฌ๋ ์ด์
- ์ต๋จ ๊ฒฝ๋ก
- ๊ตฌํ
- GCP
- ์คํ
- dynamic debugging
- java
- OS
- ๋ฌธ์์ด
- DP
- ๊ทธ๋ฆฌ๋
- ์ฌ๊ท
- JPA
- Spring
- ๋ฐ์ดํฌ์คํธ๋ผ
- ์ฐ์ ์์ ํ
- ๋ถํ ์ ๋ณต
- ์์ ์ ๋ ฌ
- BFS
- Today
- Total
hades
[Baekjoon] 5430๋ฒ: AC ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/5430
๐ ์ค๊ณ
R์ด๋ฉด ๋ค์ง๊ณ , D์ด๋ฉด ๋งจ ์์ ์๋ ์ซ์๋ฅผ ์ญ์ ํด์ผ ํ๋๋ฐ, R์ผ ๋๋ง๋ค ๋ค์ง๋๋ฐ ์๊ฐ์ด ๋ง์ด ์์๋๋ค.
๋ฐ๋ผ์, ์ ๋ค์์ ์ญ์ ๋ฅผ ํ ์ ์๋ ๋ฑ์ ์ด์ฉํ๋ค.
์ ๋ ฅ์ ๊ดํธ์ ์ผํ๊ฐ ํฌํจ๋์ด ์ ๋ ฅ๋๋ฏ๋ก, ์กฐ๊ฑด๋ฌธ์ ํตํด ์ซ์๋ค์ ๋ด์์ผ ํ๋ค.
[ ๋ผ๋ฉด ์ถ๊ฐ์ ์ธ ์ฒ๋ฆฌ๋ฅผ ํ ํ์๊ฐ ์๋ค.
, ๋๋ ] ๋ผ๋ฉด ์ซ์๋ฅผ ๋ฑ์ ๋ด๋๋ค.
์ด์ธ๋ผ๋ฉด, ์ซ์๋ฅผ ๊ตฌ์ฑํ๋ค.
์ถ๋ ฅ, ์ญ์ ํ ๋ ์์์๋ถํฐ ํ ์ง ๋ค์์๋ถํฐ ํ ์ง ๊ฒฐ์ ํ๊ธฐ ์ํด sign์ด๋ผ๋ ๋ณ์๋ฅผ ์ค์ ํ์๋ค. 0์ด๋ฉด ์์์๋ถํฐ, 1์ด๋ฉด ๋ค์์๋ถํฐ, 2์ด๋ฉด ๋น์ด์๋๋ฐ ์ญ์ ํด์ error์ธ ๊ฒฝ์ฐ์ด๋ค.
๐ ํ์ด
#include <iostream>
#include <string>
#include <deque>
using namespace std;
int t, n, sign;
deque<int> dq;
string p, temp_list, temp_num;
int main(void)
{
cin >> t;
for (int i = 0; i < t; i++) {
sign = 0;
cin >> p >> n >> temp_list;
for (int j = 0; j < temp_list.length(); j++) {
if (temp_list[j] == '[') {
continue;
}
if (temp_list[j] == ',' || temp_list[j] == ']') {
if (temp_num == "") {
continue;
}
dq.push_back(stoi(temp_num));
temp_num = "";
}
else {
temp_num += temp_list[j];
}
}
for (int j = 0; j < p.length(); j++) {
if (p[j] == 'R') {
if (sign == 1) {
sign = 0;
}
else if (sign == 0) {
sign = 1;
}
}
else if (p[j] == 'D') {
if (dq.empty()) {
sign = 2;
break;
}
if (sign == 0){
dq.pop_front();
}
else if (sign == 1) {
dq.pop_back();
}
}
}
int size = dq.size();
if (sign == 0) {
cout << "[";
for (int j = 0; j < size; j++) {
cout << dq.front();
dq.pop_front();
if (j < size - 1) {
cout << ",";
}
}
cout << "]\n";
}
else if (sign == 1) {
cout << "[";
for (int j = 0; j < size; j++) {
cout << dq.back();
dq.pop_back();
if (j < size - 1) {
cout << ",";
}
}
cout << "]\n";
}
else if (sign == 2) {
cout << "error" << "\n";
}
}
return 0;
}
๐ ๋ฉ๋ชจ
- ์๋ค ์ฝ์ ์ญ์ ๊ฐ ํ์ํ ๊ฒฝ์ฐ์๋ ๋ฑ์ ์ด์ฉํ ์ ์๋ค.
- ๋น์ด์๋๋ฐ ์ญ์ ํ๋ ๊ฒฝ์ฐ์ error๋ฅผ ๋ฐ์์ํจ๋ค. ๋น์ด์๊ธฐ๋ง ํ ๊ฒฝ์ฐ์๋ [] ๋ฅผ ์ถ๋ ฅํ๋ค.
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 7569๋ฒ: ํ ๋งํ (0) | 2024.07.16 |
---|---|
[Baekjoon] 5525๋ฒ: IOIOI (0) | 2024.07.15 |
[Baekjoon] 2667๋ฒ: ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2024.07.13 |
[Baekjoon] 2630๋ฒ: ์์ข ์ด ๋ง๋ค๊ธฐ (0) | 2024.07.11 |
[Baekjoon] 2606๋ฒ: ๋ฐ์ด๋ฌ์ค (0) | 2024.07.11 |