-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSim.cpp
More file actions
53 lines (37 loc) · 1.39 KB
/
Sim.cpp
File metadata and controls
53 lines (37 loc) · 1.39 KB
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int T; cin >> T;
cin.ignore();
while (T--) {
list<char> sentence;
list<char>::iterator itr;
string collection;
getline(cin, collection);
itr = sentence.begin();
for (char i : collection) {
if (i == '<') {
if (itr != sentence.begin()) itr = sentence.erase(--itr);
// if backspace when at front of the list, it should do nothing.
// Erase: Erases current itr element and returns following itr pos.
// Since itr is always pointing to next element, we decrement.
} else if (i == '[') {
itr = sentence.begin();
} else if (i == ']') {
if (sentence.empty()) itr == sentence.begin();
else itr = sentence.end();
} else {
itr = sentence.insert(itr, i);
// Insert: Inserts element before itr and returns itr pointing to the inserted element.
// Since itr is always pointing to next element, we can insert and increment afterwards to maintain itr pos after referenced element.
++itr;
}
}
for (char j : sentence) {
cout << j;
}
cout << '\n';
}
return 0;
}