This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_reverse.cpp
287 lines (249 loc) · 6.07 KB
/
is_reverse.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdexcept>
#include <iostream>
namespace CP
{
template <typename T>
class vector
{
public:
typedef T *iterator;
protected:
T *mData;
size_t mCap;
size_t mSize;
void rangeCheck(int n)
{
if (n < 0 || (size_t)n >= mSize)
{
throw std::out_of_range("index of out range");
}
}
void expand(size_t capacity)
{
T *arr = new T[capacity]();
for (size_t i = 0; i < mSize; i++)
{
arr[i] = mData[i];
}
delete[] mData;
mData = arr;
mCap = capacity;
}
public:
//-------------- constructor & copy operator ----------
// copy constructor
vector(const vector<T> &a)
{
mData = new T[a.capacity()]();
mCap = a.capacity();
mSize = a.size();
for (size_t i = 0; i < a.size(); i++)
{
mData[i] = a[i];
}
}
// default constructor
vector()
{
int cap = 1;
mData = new T[cap]();
mCap = cap;
mSize = 0;
}
// constructor with initial size
vector(size_t cap)
{
mData = new T[cap]();
mCap = cap;
mSize = cap;
}
// copy assignment operator using copy-and-swap idiom
vector<T> &operator=(vector<T> other)
{
// other is copy-constructed which will be destruct at the end of this scope
// we swap the content of this class to the other class and let it be descructed
using std::swap;
swap(this->mSize, other.mSize);
swap(this->mCap, other.mCap);
swap(this->mData, other.mData);
return *this;
}
~vector()
{
clear();
delete[] mData;
}
//------------- capacity function -------------------
bool empty() const
{
return mSize == 0;
}
size_t size() const
{
return mSize;
}
size_t capacity() const
{
return mCap;
}
void resize(size_t n)
{
if (n > mCap)
expand(n);
if (n > mSize)
{
T init = T();
for (size_t i = mSize; i < n; i++)
mData[i] = init;
}
mSize = n;
}
//----------------- iterator ---------------
iterator begin()
{
return &mData[0];
}
iterator end()
{
return begin() + mSize;
}
//----------------- access -----------------
T &at(int index)
{
rangeCheck(index);
return mData[index];
}
T &at(int index) const
{
rangeCheck(index);
return mData[index];
}
T &operator[](int index)
{
return mData[index];
}
T &operator[](int index) const
{
return mData[index];
}
//----------------- modifier -------------
void push_back(const T &element)
{
insert(end(), element);
}
void pop_back()
{
mSize--;
}
iterator insert(iterator it, const T &element)
{
size_t pos = it - begin();
if (mSize + 1 > mCap)
expand(2 * mCap);
for (size_t i = mSize; i > pos; i--)
{
mData[i] = mData[i - 1];
}
mData[pos] = element;
mSize++;
return begin() + pos;
}
void erase(iterator it)
{
while ((it + 1) != end())
{
*it = *(it + 1);
it++;
}
mSize--;
}
void clear()
{
mSize = 0;
}
//-------------- extra (unlike STL) ------------------
void insert_by_pos(size_t it, const T &element)
{
insert(begin() + it, element);
}
void erase_by_pos(int index)
{
erase(begin() + index);
}
void erase_by_value(const T &element)
{
int i = index_of(element);
if (i != -1)
erase_by_pos(i);
}
bool contains(const T &element)
{
return index_of(element) != -1;
}
int index_of(const T &element)
{
for (int i = 0; i < mSize; i++)
{
if (mData[i] == element)
{
return i;
}
}
return -1;
}
bool isReverse(const vector<T> &other) const
{
// write your code only in this function
if (size() != other.size())
return false;
T *it1 = mData;
T *it2 = other.mData + size() - 1;
while (it1 != mData + size())
{
if (*it1 != *it2)
return false;
++it1;
--it2;
}
return true;
}
};
} // end namespace
bool equal(CP::vector<int> &a, CP::vector<int> &b)
{
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); i++)
{
if (a[i] != b[i])
return false;
}
return true;
}
int main()
{
int n, m;
std::cin >> n >> m;
// read input
CP::vector<int> a, b;
while (n--)
{
int tmp;
std::cin >> tmp;
a.push_back(tmp);
}
while (m--)
{
int tmp;
std::cin >> tmp;
b.push_back(tmp);
}
// check
CP::vector<int> c;
c = b;
std::cout << a.isReverse(b) << std::endl;
std::cout << equal(c, b) << std::endl;
c = a;
std::cout << b.isReverse(a) << std::endl;
std::cout << equal(c, a) << std::endl;
}