-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintegerlists.cpp
66 lines (61 loc) · 1.21 KB
/
integerlists.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
#include <bits/stdc++.h>
using namespace std;
using dqi = deque<int>;
dqi parse(string &line) {
dqi Q;
stringstream ss(line.substr(1, line.size() - 2));
string token;
while (getline(ss, token, ',')) Q.push_back(stoi(token));
return Q;
}
bool update(char cmd, dqi &Q, bool &rev) {
if (cmd == 'R') {
rev = !rev;
return true;
}
if (Q.empty()) return false;
if (!rev) Q.pop_front();
else Q.pop_back();
return true;
}
void print(dqi &Q, bool rev) {
cout << '[';
bool f = true;
if (!rev) {
for (auto q : Q) {
if (!f) cout << ',';
f = false;
cout << q;
}
} else {
for (auto itr = Q.rbegin(); itr != Q.rend(); ++itr) {
if (!f) cout << ',';
f = false;
cout << *itr;
}
}
cout << ']' << endl;
}
int main() {
int t;
string line;
getline(cin, line);
t = stoi(line);
while (t--) {
string cmd;
getline(cin, cmd);
getline(cin, line);
getline(cin, line);
bool rev = false, error = false;
auto Q = parse(line);
for (size_t i = 0; i < cmd.size(); ++i) {
if (!update(cmd[i], Q, rev)) {
error = true;
break;
}
}
if (error) cout << "error" << endl;
else print(Q, rev);
}
return 0;
}