-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegerlists.cpp
More file actions
116 lines (100 loc) · 3.08 KB
/
integerlists.cpp
File metadata and controls
116 lines (100 loc) · 3.08 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Fast IO
// (Required to not get time limit exceeded)
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// For each case
int t;
cin >> t;
while(t-- > 0)
{
// Get the functions to perform
string functions;
cin >> functions;
// Get the starting size of the list
int sLen;
cin >> sLen;
vector<int> list(sLen); // Create a vector to store the list
char rm; // Character used to store unused pieces of input strings
// If the list doesn't contain any elements
if(sLen == 0)
{
cin >> rm; // Get the '['
cin >> rm; // Get the ']'
}
else
{
cin >> rm; // Get the '['
// For each element
for(int i = 0; i < sLen; i++)
{
cin >> list[i]; // Get the element and put it in the vector
cin >> rm; // Get the ',' or ']'
}
}
bool front = true; // Whether the vector is being accessed from the front
bool error = false; // Whether an error has occurred
// For each function in the function string
for(char function : functions)
{
// If it's a reverse function
if(function == 'R')
{
// Set the vector to be accessed from the opposite side
front = !front;
}
// If it's a delete function
else
{
// If the list is empty
if(list.empty())
{
// Can't delete anything so set error and break
error = true;
break;
}
// If the list is being accessed from the front
if(front)
{
// Delete the front element
list.erase(list.begin());
}
// If the list is being accessed from the back
else
{
// Delete the back element
list.erase(--list.end());
}
}
}
// If there wasn't an error
if(!error)
{
// Print the list
cout << "[";
// If the list isn't empty, print its elements
if(!list.empty())
{
// Print the first element
// (if accessing from the front 0, otherwise the size - 1)
cout << list[front ? 0 : list.size()-1];
// For any other elements (2nd onwards)
for(int i = 1; i < list.size(); i++)
{
// (if accessing from the front i, otherwise the size - i - 1)
cout << "," << list[front ? i : list.size()-i-1];
}
}
cout << "]" << endl;
}
// If there was an error
else
{
cout << "error" << endl;
}
}
return 0;
}