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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
| // MVS 2015 C++14
#include <iostream>
#include <sstream> // std::ostringstream
#include <string> // std::string
#include <stdexcept> // std::out_of_range
#include <algorithm> // std::max
#include <iomanip> // std::setw
#include <utility> // std::pair
#include <map> // std::map
#include <cstring> // std::strlen
template<typename _Ty>
class Matrix
{
private:
_Ty *mData;
unsigned mRows;
unsigned mCols;
public:
Matrix(unsigned rows, unsigned cols)
{
mRows = rows;
mCols = cols;
mData = new _Ty[rows * cols]{};
}
Matrix(const Matrix &other)
: mData{ new _Ty[other.mRows * other.mCols] },
mRows{ other.mRows }, mCols{other.mCols}
{
for (unsigned i{}; i < (other.mRows * other.mCols); ++i)
{
mData[i] = other.mData[i];
}
}
template<typename _Ty2>
Matrix(const Matrix<_Ty2> &other)
: mData{ new _Ty[other.sizeRows() * other.sizeCols()] },
mRows{ other.sizeRows() }, mCols{ other.sizeCols() }
{
for (unsigned i{}; i < other.sizeRows(); ++i)
{
for (unsigned j{}; j < other.sizeCols(); ++j)
{
this->get(i, j) = other.get(i, j);
}
}
}
Matrix& operator=(const Matrix &rhs)
{
if (this != &rhs)
{
delete[] mData;
mData = new _Ty[rhs.mRows * rhs.mCols];
mRows = rhs.mRows;
mCols = rhs.mCols;
for (unsigned i{}; i < (rhs.mRows * rhs.mCols); ++i)
{
mData[i] = rhs.mData[i];
}
}
return *this;
}
template<typename _Ty2>
Matrix& operator=(const Matrix<_Ty2> &rhs)
{
if (this != &rhs)
{
delete[] mData;
mData = new _Ty[rhs.sizeRows() * rhs.sizeCols()];
mRows = rhs.sizeRows();
mCols = rhs.sizeCols();
for (unsigned i{}; i < rhs.sizeRows(); ++i)
{
for (unsigned j{}; j < rhs.sizeCols(); ++j)
{
this->get(i, j) = rhs.get(i, j);
}
}
}
return *this;
}
unsigned sizeRows(void) const
{
return mRows;
}
unsigned sizeCols(void) const
{
return mCols;
}
_Ty& get(const unsigned &i, const unsigned &j)
{
if ((i > (mRows - 1)) || (j > (mCols - 1)))
{
throw std::out_of_range("index of matrix out of range");
}
return mData[(i * mRows) + j];
}
const _Ty& get(const unsigned &i, const unsigned &j) const
{
if ((i > (mRows - 1)) || (j > (mCols - 1)))
{
throw std::out_of_range("index of matrix out of range");
}
return mData[(i * mRows) + j];
}
~Matrix(void)
{
delete[] mData;
}
};
namespace Dynamic
{
enum class Offset{ NONE, LEFT, UP, DIAGONALY}; // смещение в таблице (для восстановления строки)
inline
std::pair<int, std::string> LCS(const char *_firstStr, const char *_secondStr,
const unsigned &_firstLength, const unsigned &_secondLength, const int &w)
{
Matrix<int> L{ _firstLength + 1, _secondLength + 1 };
Matrix<unsigned> kx{ _firstLength + 1, _secondLength + 1 };
Matrix<unsigned> ky{ _firstLength + 1, _secondLength + 1 };
Matrix<Offset> data{ _firstLength + 1, _secondLength + 1};
for (unsigned i{}; i < (_firstLength + 1); i++)
{
for (unsigned j{}; j < (_secondLength + 1); j++)
{
if (!i || !j)
{
L.get(i, j) = 0;
kx.get(i, j) = 0;
ky.get(i, j) = 0;
data.get(i, j) = Offset::NONE;
}
else if (_firstStr[i - 1] == _secondStr[j - 1])
{
const int Score = L.get(i - 1, j - 1) + 1 + w
* (((kx.get(i - 1, j - 1) == (i - 1)) ? 0 : 1)
- ((ky.get(i - 1, j - 1) == (j - 1)) ? 0 : 1));
const int max = std::max({ Score, L.get(i - 1, j), L.get(i, j - 1) });
if (max == Score)
{
kx.get(i, j) = i;
ky.get(i, j) = j;
L.get(i, j) = Score;
data.get(i, j) = Offset::DIAGONALY;
}
else if (max == L.get(i - 1, j))
{
kx.get(i, j) = kx.get(i - 1, j);
ky.get(i, j) = ky.get(i - 1, j);
L.get(i, j) = L.get(i - 1, j);
data.get(i, j) = Offset::UP;
}
else
{
kx.get(i, j) = kx.get(i, j - 1);
ky.get(i, j) = ky.get(i, j - 1);
L.get(i, j) = L.get(i, j - 1);
data.get(i, j) = Offset::LEFT;
}
}
else
{
if (L.get(i - 1, j) > L.get(i, j - 1))
{
kx.get(i, j) = kx.get(i - 1, j);
ky.get(i, j) = ky.get(i - 1, j);
L.get(i, j) = L.get(i - 1, j);
data.get(i, j) = Offset::UP;
}
else
{
kx.get(i, j) = kx.get(i, j - 1);
ky.get(i, j) = ky.get(i, j - 1);
L.get(i, j) = L.get(i, j - 1);
data.get(i, j) = Offset::LEFT;
}
}
}
}
std::ostringstream osstr{};
unsigned i = _firstLength;
unsigned j = _secondLength;
while (i > 0 && j > 0)
{
if (data.get(i, j) == Offset::DIAGONALY)
{
osstr << _firstStr[i - 1];
i--; j--;
}
else if (data.get(i, j) == Offset::LEFT)
{
j--;
}
else if (data.get(i, j) == Offset::UP)
{
i--;
}
}
std::string strRes{ std::move(osstr.str()) };
std::reverse(strRes.begin(), strRes.end());
/*
///================================== Вывод матриц =======================================
auto print = [=](const Matrix<int> &_matrix, const unsigned &_spaceNum)
{
for (unsigned i{}; i < _matrix.sizeRows(); ++i)
{
for (unsigned j{}; j < _matrix.sizeCols(); ++j)
{
std::cout << std::setw(_spaceNum) << _matrix.get(i, j) << std::setw(0) << ' ';
}
std::cout << std::endl;
}
};
std::cout << std::endl << "------------------[ L ]--------------------" << std::endl;
print(L, 4);
std::cout << "------------------[ kx ]-------------------" << std::endl;
print(kx, 4);
std::cout << "------------------[ ky ]-------------------" << std::endl;
print(ky, 4);
std::cout << "------------------[ Data ]--------------------" << std::endl;
for (unsigned i{}; i < data.sizeRows(); ++i)
{
for (unsigned j{}; j < data.sizeCols(); ++j)
{
switch (data.get(i, j))
{
case Offset::NONE:
std::cout << "0 ";
break;
case Offset::DIAGONALY:
std::cout << "D ";
break;
case Offset::LEFT:
std::cout << "L ";
break;
case Offset::UP:
std::cout << "U ";
break;
}
}
std::cout << std::endl;
}
std::cout << "-------------------------------------------" << std::endl;
///=======================================================================================
//*/
return{ L.get(_firstLength, _secondLength), strRes };
}
}
template<typename _Ty, typename _Ty2, typename _Ty3>
struct Triad
{
// Переменные структуры
_Ty first; // 1-я переменная. score
_Ty2 second; /// 2-я переменная. i
_Ty3 third; /// 3-я переменная. j
// Определения типов
typedef _Ty first_type;
typedef _Ty2 second_type;
typedef _Ty3 third_type;
};
namespace Recursion
{
using triad = Triad<int, int, int>; // Используем 'triad' вместо того, чтобы каждый раз писать аргументы шаблона
inline
triad LCS(const char *_firstStr, const char *_secondStr,
const int &_idx_i, const int &_idx_j, const int &w)
{
if (!_idx_i || !_idx_j)
{
return{ 0, -1 , -1 };
}
triad res[]
{
LCS(_firstStr, _secondStr, _idx_i - 1, _idx_j, w),
LCS(_firstStr, _secondStr, _idx_i, _idx_j - 1, w)
};
if (_firstStr[_idx_i - 1] == _secondStr[_idx_j - 1])
{
const triad subTaskOnScore = LCS(_firstStr, _secondStr, _idx_i - 1, _idx_j - 1, w);
const int Score = subTaskOnScore.first + 1 + w
* (((subTaskOnScore.second == (_idx_i - 1)) ? 0 : 1)
- ((subTaskOnScore.third == (_idx_j - 1)) ? 0 : 1));
const int Max = std::max({ Score, res[0].first, res[1].first });
if (Max == Score)
{
return{ Score, _idx_i, _idx_j };
}
else if (Max == res[0].first)
{
return res[0];
}
else
{
return res[1];
}
}
else
{
return std::max(res[0], res[1],
[=](const triad &lhs, const triad &rhs) -> bool
{
return (lhs.first < rhs.first);
});
}
}
}
namespace Memoization
{
using triad = Triad<int, int, int>;
std::map<std::pair<int, int>, triad> globalMemoryScore;
inline
triad _lcs(const char *_firstStr, const char *_secondStr,
const int &_idx_i, const int &_idx_j, const int &w)
{
const std::pair<int, int> Key{ _idx_i, _idx_j };
if (globalMemoryScore.count(Key))
{
return globalMemoryScore[Key];
}
if (!_idx_i || !_idx_j)
{
return (globalMemoryScore[Key] = { 0, -1 , -1 });
}
triad res[]
{
_lcs(_firstStr, _secondStr, _idx_i - 1, _idx_j, w),
_lcs(_firstStr, _secondStr, _idx_i, _idx_j - 1, w)
};
if (_firstStr[_idx_i - 1] == _secondStr[_idx_j - 1])
{
const triad subTaskOnScore = _lcs(_firstStr, _secondStr, _idx_i - 1, _idx_j - 1, w);
const int Score = subTaskOnScore.first + 1 + w
* (((subTaskOnScore.second == (_idx_i - 1)) ? 0 : 1)
- ((subTaskOnScore.third == (_idx_j - 1)) ? 0 : 1));
const int Max = std::max({ Score, res[0].first, res[1].first });
if (Max == Score)
{
return (globalMemoryScore[Key] = { Score, _idx_i , _idx_j });
}
else if (Max == res[0].first)
{
return (globalMemoryScore[Key] = res[0]);
}
else
{
return (globalMemoryScore[Key] = res[1]);
}
}
else
{
return (globalMemoryScore[Key] = std::max(res[0], res[1],
[=](const triad &lhs, const triad &rhs) -> bool
{
return (lhs.first < rhs.first);
}));
}
}
inline
triad LCS(const char *x, const char *y,
const int &_idx_i, const int &j, const int &w)
{
globalMemoryScore.clear();
return _lcs(x, y, _idx_i, j, w);
}
}
int main()
{
char firstStr[]{ "FABABCCCC" };
char secondStr[]{ "FCBCBCACA" };
std::cout << "Str #1: " << firstStr << std::endl;
std::cout << "Str #2: " << secondStr << std::endl;
const int w = 0;
const unsigned fLen = std::strlen(firstStr);
const unsigned sLen = std::strlen(secondStr);
auto resDynamic = Dynamic::LCS(firstStr, secondStr, fLen, sLen, w);
std::cout << "Dynamic: " << resDynamic.first << "\t" << resDynamic.second << std::endl;
std::cout << "Recurcione: " << Recursion::LCS(firstStr, secondStr, fLen, sLen, w).first << std::endl;
std::cout << "Memoization: " << Memoization::LCS(firstStr, secondStr, fLen, sLen, w).first << std::endl;
} |