-
Notifications
You must be signed in to change notification settings - Fork 0
/
1406-stack.cpp
58 lines (49 loc) · 1.14 KB
/
1406-stack.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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
string statement;
getline(cin, statement);
stack<char> left, right;
for (char c : statement)
left.push(c);
int M;
cin >> M;
cin.ignore();
for (int i = 0; i < M; ++i) {
string command;
getline(cin, command);
switch (command[0]) {
case 'L':
if (!left.empty()) {
right.push(left.top());
left.pop();
}
break;
case 'D':
if (!right.empty()) {
left.push(right.top());
right.pop();
}
break;
case 'B':
if (!left.empty()) {
left.pop();
}
break;
case 'P':
left.push(command[2]);
break;
}
}
while (!left.empty()) {
right.push(left.top());
left.pop();
}
while (!right.empty()) {
cout << right.top();
right.pop();
}
return 0;
}