반응형
문제
해결 방법
삭제할때 pop을 통하여 삭제할 예정이기 때문에 특수문자와 자릿수 문제 해결을 위하여 숫자들을 모두 deque<string에 담는다. 이후 짝수갯씩 붙어있는 R은 의미가 없기 때문에 미리 삭제해주며, 이후 R은 직접 뒤집지 않고 뒤집혔는가?의 여부만 체크하며 D에서는 이 값에 따라서 pop_front/pop_back을 결정한다.
아쉬웠던 점
짝수갯식 붙어있는 R정리해주는 행동은 해도 안해도 시간은 똑같더라 ㅠ 왜지?
코드
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <deque>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int Num;
cin >> Num;
for (int i = 0; i < Num; ++i)
{
//입력
string Command_Str, Str, Save_Str = "";
cin >> Command_Str;
int Ary_Num;
cin >> Ary_Num;
cin >> Str;
deque<string> Deq;
//string에 있는 숫자들 deque로 옮김
for (int k = 1; k < Str.size(); ++k)
{
if (!Save_Str.empty() && (Str[k] == ',' || Str[k] == ']'))
{
Deq.emplace_back(Save_Str);
Save_Str = "";
}
else
Save_Str += Str[k];
}
Str = Command_Str;
Command_Str = "";
//짝수갯씩 붙어있는 R은 의미가 없기 때문에 미리 삭제해줌
int Count = 0;
for (int j = 0; j < Str.size(); ++j)
{
if (Str[j] == 'R')
Count++;
else
{
if (Count % 2 == 1)
Command_Str += 'R';
Count = 0;
Command_Str += Str[j];
}
}
if (Count % 2 == 1)
Command_Str += 'R';
//작동
bool Check = true, Reverse_Check = false;
for (int j = 0; j < Command_Str.size(); ++j)
{
if (Command_Str[j] == 'R')
{
if (Reverse_Check)
Reverse_Check = false;
else
Reverse_Check = true;
}
else if (Command_Str[j] == 'D')
{
if (Ary_Num == 0)
{
cout << "error" << '\n';
Check = false;
break;
}
if(Reverse_Check)
Deq.pop_back();
else
Deq.pop_front();
Ary_Num--;
}
}
//출력
if (Check)
{
if (Reverse_Check)
reverse(Deq.begin(), Deq.end());
cout << '[';
for (int j = 0; j < (int)Deq.size() - 1; j++)
cout << Deq[j] << ',';
if(Deq.size() != 0)
cout << Deq[Deq.size() - 1];
cout << ']' << '\n';
}
}
}
|
cs |
잡설 : 정답이 [1,2,3,4]형식인데 [1, 2, 3, 4]로 출력하고 있어서 한시간 삽질했다...ㅠ
반응형
'코딩 > 문제풀이-백준' 카테고리의 다른 글
백준 9661번 - 돌 게임 7 (C++) (0) | 2021.10.02 |
---|---|
백준 11438번 - LCA2 (C++) (0) | 2021.10.02 |
백준 1700번 - 멀티탭 스케줄링 (C++) (0) | 2021.09.17 |
백준 19237번 - 어른 상어 (C++) (1) | 2021.08.20 |
백준 1430번 - 공격 (C++) (0) | 2021.08.07 |
댓글