54# include <string_view>
61 namespace string_dp_detail
66 for (
size_t j = 0; j < cols; ++j)
77 for (
size_t i = 0; i < rows; ++i)
95 const std::string_view b)
102 std::string_view
s1 = a;
103 std::string_view
s2 = b;
104 if (
s1.size() >
s2.size())
110 for (
size_t j = 0; j <=
s1.size(); ++j)
116 for (
size_t j = 0; j <=
s1.size(); ++j)
119 for (
size_t i = 1; i <=
s2.size(); ++i)
123 for (
size_t j = 1; j <=
s1.size(); ++j)
125 const size_t cost =
s2[i - 1] ==
s1[j - 1] ? 0 : 1;
136 return prev[
s1.size()];
148 const std::string_view b)
168 const std::string_view b)
170 const size_t n = a.size();
171 const size_t m = b.
size();
173 const size_t inf = n +
m;
178 for (
size_t i = 0; i <= n; ++i)
184 for (
size_t j = 0; j <=
m; ++j)
190 std::array<size_t, 256> last_row = {0};
192 for (
size_t i = 1; i <= n; ++i)
196 for (
size_t j = 1; j <=
m; ++j)
198 const size_t i1 = last_row[
static_cast<unsigned char>(b[j - 1])];
202 if (a[i - 1] == b[j - 1])
208 d[i + 1][j + 1] = std::min({
212 d[
i1][
j1] + (i -
i1 - 1) + 1 + (j -
j1 - 1)
216 last_row[
static_cast<unsigned char>(a[i - 1])] = i;
219 return d[n + 1][
m + 1];
242 const std::string_view b)
244 const size_t n = a.size();
245 const size_t m = b.
size();
250 for (
size_t i = 1; i <= n; ++i)
251 for (
size_t j = 1; j <=
m; ++j)
252 if (a[i - 1] == b[j - 1])
253 dp[i][j] = dp[i - 1][j - 1] + 1;
255 dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
258 seq.reserve(dp[n][
m]);
262 while (i > 0
and j > 0)
263 if (a[i - 1] == b[j - 1])
265 seq.push_back(a[i - 1]);
269 else if (dp[i - 1][j] >= dp[i][j - 1])
274 std::reverse(seq.begin(), seq.end());
302 const std::string_view b)
304 const size_t n = a.size();
305 const size_t m = b.
size();
310 for (
size_t j = 0; j <=
m; ++j)
320 for (
size_t i = 1; i <= n; ++i)
322 for (
size_t j = 1; j <=
m; ++j)
323 if (a[i - 1] == b[j - 1])
325 curr[j] = prev[j - 1] + 1;
337 for (
size_t j = 0; j <=
m; ++j)
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void swap(Array &s) noexcept
Swap this with s
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_j1_function > > j1(const __gmp_expr< T, U > &expr)
Array< Array< size_t > > make_zero_matrix(const size_t rows, const size_t cols)
Array< size_t > make_zero_row(const size_t cols)
Main namespace for Aleph-w library functions.
LCS_Result longest_common_subsequence(const std::string_view a, const std::string_view b)
Compute Longest Common Subsequence.
and
Check uniqueness with explicit hash + equality functors.
size_t levenshtein_distance(const std::string_view a, const std::string_view b)
Levenshtein distance (insert/delete/substitute each cost 1).
size_t edit_distance(const std::string_view a, const std::string_view b)
Alias for Levenshtein distance.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Longest_Common_Substring_Result longest_common_substring(const std::string_view a, const std::string_view b)
Compute the longest common substring (contiguous) between two strings.
size_t damerau_levenshtein_distance(const std::string_view a, const std::string_view b)
Damerau-Levenshtein distance with adjacent transpositions.
Result for Longest Common Subsequence (LCS).
std::string subsequence
The subsequence string.
size_t length
Length of the subsequence.
Result for longest common substring.
size_t begin_a
Start index in first string.
size_t length
Length of common substring.
std::string substring
The common substring.
size_t begin_b
Start index in second string.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.