-
Notifications
You must be signed in to change notification settings - Fork 0
/
5430.cpp
82 lines (63 loc) · 1.7 KB
/
5430.cpp
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
#include <iostream>
#include <deque>
#include <string>
#include <sstream>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
string p;
cin >> p;
int n;
cin >> n;
string x;
cin >> x;
istringstream strm(x.substr(1, x.size() - 2));
deque<int> dq;
while (getline(strm, x, ','))
dq.push_back(stoi(x));
bool back = false, is_error = false;
for (string::size_type i = 0; i < p.size(); ++i) {
string::value_type c = p[i];
if (c == 'R')
back = !back;
else if (c == 'D') {
if (dq.size() <= 0) {
is_error = true;
break;
}
if (!back)
dq.pop_front();
else
dq.pop_back();
}
}
if (is_error)
cout << "error\n";
else {
cout << '[';
for (deque<int>::size_type i = dq.size(); i > 1; --i) {
int number = 0;
if (!back) {
number = dq.front();
dq.pop_front();
dq.push_back(number);
} else {
number = dq.back();
dq.pop_back();
dq.push_front(number);
}
cout << number << ',';
}
if (!dq.empty()) {
if (!back)
cout << dq.front();
else
cout << dq.back();
}
cout << "]\n";
}
}
return 0;
}