-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdynamic_programming.hpp
300 lines (255 loc) · 9.07 KB
/
dynamic_programming.hpp
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
/**
* @file dynamic_programming.hpp
* @brief
* @date 2018-4-23
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_ALGORITHM_DYNAMIC_PROGRAMMING_HPP
#define KERBAL_ALGORITHM_DYNAMIC_PROGRAMMING_HPP
#include <kerbal/algorithm/binary_search/lower_bound.hpp>
#include <kerbal/algorithm/modifier/fill.hpp>
#include <kerbal/algorithm/modifier/iota.hpp>
#include <kerbal/compare/basic_compare.hpp>
#include <kerbal/compare/binary_type_compare.hpp>
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/container/nonmember_container_access.hpp>
#include <kerbal/container/vector.hpp>
#include <kerbal/iterator/general_back_inserter.hpp>
#include <kerbal/iterator/iterator.hpp>
#include <cstddef>
namespace kerbal
{
namespace algorithm
{
// warning: Buffer must have the size >= distance(a_first, a_last) + 1
template <
typename ForwardIterator1, typename InputIterator2,
typename BinaryTypeEqualTo, typename ForwardBufferIterator
>
KERBAL_CONSTEXPR14
std::size_t longest_common_subsequence(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last,
BinaryTypeEqualTo equal_to,
ForwardBufferIterator buffer_first
)
{
typedef ForwardIterator1 iterator1;
typedef InputIterator2 iterator2;
typedef ForwardBufferIterator buffer_iterator;
buffer_iterator buffer_back_iter(
kerbal::iterator::next(buffer_first, kerbal::iterator::distance(a_first, a_last))
);
kerbal::algorithm::fill(buffer_first,
kerbal::iterator::next(buffer_back_iter), static_cast<std::size_t>(0)
);
for (iterator2 i(b_first); i != b_last; ++i) {
std::size_t dp_i1_j1 = 0; // dp[i - 1][j - 1]
buffer_iterator k(buffer_first);
buffer_iterator k_next(kerbal::iterator::next(buffer_first));
for (iterator1 j(a_first); j != a_last; ++j) {
if (equal_to(*j, *i)) {
std::size_t tmp = *k_next;
*k_next = dp_i1_j1 + 1;
dp_i1_j1 = tmp;
} else {
dp_i1_j1 = *k_next;
if (*k > *k_next) {
*k_next = *k;
}
}
k = k_next;
++k_next;
}
}
return *buffer_back_iter;
}
template <typename ForwardIterator1, typename InputIterator2, typename BinaryTypeEqualTo>
std::size_t longest_common_subsequence(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last,
BinaryTypeEqualTo equal_to
)
{
typedef typename kerbal::iterator::iterator_traits<ForwardIterator1>::difference_type difference_type;
difference_type buffer_size_need(kerbal::iterator::distance(a_first, a_last) + 1);
if (buffer_size_need > 128) {
kerbal::container::vector<std::size_t> buffer(buffer_size_need);
return kerbal::algorithm::longest_common_subsequence(
a_first, a_last,
b_first, b_last,
equal_to, buffer.begin()
);
} else {
std::size_t buffer[128];
return kerbal::algorithm::longest_common_subsequence(
a_first, a_last,
b_first, b_last,
equal_to, buffer + 0
);
}
}
template <typename ForwardIterator1, typename InputIterator2>
std::size_t longest_common_subsequence(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last
)
{
typedef typename std::iterator_traits<ForwardIterator1>::value_type value_type1;
typedef typename std::iterator_traits<InputIterator2>::value_type value_type2;
return kerbal::algorithm::longest_common_subsequence(
a_first, a_last, b_first, b_last,
kerbal::compare::binary_type_equal_to<value_type1, value_type2>()
);
}
namespace detail
{
template <typename ForwardIterator, typename Compare, typename Container, typename BackInserter>
KERBAL_CONSTEXPR14
std::size_t longest_increasing_subsequence_helper(
ForwardIterator first, ForwardIterator last, Compare cmp,
Container & buffer, BackInserter back_inserter
)
{
if (first == last) {
return 0;
}
typedef ForwardIterator iterator;
struct iter_cmp
{
Compare cmp;
KERBAL_CONSTEXPR
iter_cmp(Compare cmp) :
cmp(cmp)
{
}
KERBAL_CONSTEXPR14
bool operator()(iterator a, iterator b)
{
return this->cmp(*a, *b);
}
} _iter_cmp(cmp);
*back_inserter = first; ++back_inserter;
std::size_t index_of_back = 0;
++first;
for (; first != last; ++first) {
if (cmp(buffer[index_of_back], *first)) {
*back_inserter = first; ++back_inserter; ++index_of_back;
} else {
*kerbal::algorithm::lower_bound(
kerbal::container::begin(buffer), kerbal::container::end(buffer), first, _iter_cmp
) = first;
}
}
return index_of_back + 1;
}
} // namespace detail
//warning: buffer must have capacity >= distance(first, last)
template <typename ForwardIterator, typename Compare, typename Container>
KERBAL_CONSTEXPR14
std::size_t longest_increasing_subsequence(
ForwardIterator first, ForwardIterator last, Compare cmp,
Container & buffer
)
{
return kerbal::algorithm::detail::longest_increasing_subsequence_helper(
first, last, cmp, buffer, kerbal::iterator::general_inserter(buffer)
);
}
template <typename ForwardIterator, typename Compare>
std::size_t longest_increasing_subsequence(ForwardIterator first, ForwardIterator last, Compare cmp)
{
typedef ForwardIterator iterator;
typedef typename kerbal::iterator::iterator_traits<iterator>::difference_type difference_type;
difference_type buffer_size_need(kerbal::iterator::distance(first, last));
if (buffer_size_need > 32) {
kerbal::container::vector<iterator> buffer;
buffer.reserve(buffer_size_need);
return kerbal::algorithm::longest_increasing_subsequence(first, last, cmp, buffer);
} else {
iterator buffer[32];
return kerbal::algorithm::longest_increasing_subsequence(first, last, cmp, buffer);
}
}
template <typename ForwardIterator>
std::size_t longest_increasing_subsequence(ForwardIterator first, ForwardIterator last)
{
typedef ForwardIterator iterator;
typedef typename std::iterator_traits<iterator>::value_type value_type;
return kerbal::algorithm::longest_increasing_subsequence(first, last, kerbal::compare::less<value_type>());
}
// warning: buffer must have the size >= std::distance(a_first, a_last) + 1
template <typename ForwardIterator1, typename InputIterator2,
typename BinaryTypeEqualTo, typename ForwardBufferIterator>
KERBAL_CONSTEXPR14
std::size_t edit_distance(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last,
BinaryTypeEqualTo equal, ForwardBufferIterator buffer_first
)
{
typedef ForwardIterator1 iterator1;
typedef InputIterator2 iterator2;
typedef ForwardBufferIterator buffer_iterator;
buffer_iterator buffer_back_iter(kerbal::iterator::next(buffer_first, kerbal::iterator::distance(a_first, a_last)));
kerbal::algorithm::iota(buffer_first, kerbal::iterator::next(buffer_back_iter), static_cast<std::size_t>(0));
std::size_t i_index = 0;
for (; b_first != b_last; ++b_first) {
std::size_t dp_i1_j1 = i_index;
++i_index;
buffer_iterator k_prev(buffer_first);
buffer_iterator k(kerbal::iterator::next(buffer_first));
*k_prev = i_index;
for (iterator1 j(a_first); j != a_last; ++j) {
std::size_t modify = dp_i1_j1 + (equal(*j, *b_first) ? 0 : 1);
dp_i1_j1 = *k;
if (*k_prev < *k) {
*k = *k_prev;
}
++(*k);
if (modify < *k) {
*k = modify;
}
k_prev = k;
++k;
}
}
return *buffer_back_iter;
}
template <typename ForwardIterator1, typename InputIterator2, typename BinaryTypeEqualTo>
std::size_t edit_distance(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last,
BinaryTypeEqualTo equal
)
{
typedef typename kerbal::iterator::iterator_traits<ForwardIterator1>::difference_type difference_type;
difference_type buffer_size_need(kerbal::iterator::distance(a_first, a_last) + 1);
if (buffer_size_need > 32) {
kerbal::container::vector<std::size_t> buffer(buffer_size_need);
return kerbal::algorithm::edit_distance(a_first, a_last, b_first, b_last, equal, buffer.begin());
} else {
std::size_t buffer[32];
return kerbal::algorithm::edit_distance(a_first, a_last, b_first, b_last, equal, buffer + 0);
}
}
template <typename ForwardIterator1, typename InputIterator2>
std::size_t edit_distance(
ForwardIterator1 a_first, ForwardIterator1 a_last,
InputIterator2 b_first, InputIterator2 b_last
)
{
typedef typename std::iterator_traits<ForwardIterator1>::value_type value_type1;
typedef typename std::iterator_traits<InputIterator2>::value_type value_type2;
return kerbal::algorithm::edit_distance(
a_first, a_last, b_first, b_last,
kerbal::compare::binary_type_equal_to<value_type1, value_type2>()
);
}
} // namespace algorithm
} // namespace kerbal
#endif // KERBAL_ALGORITHM_DYNAMIC_PROGRAMMING_HPP