description: Answer LCS lengths between prefixes of one sequence and substrings of another. The dynamic-programming grid is represented by its seaweed permutation: after adding one symbol of the first sequence, a single scan swaps boundary endpoints exactly where equality or crossing changes the next LCS row. For a fixed prefix, the LCS deficit of [left, right) equals the number of active endpoints entering that interval from before left. Sweeping right and counting endpoint positions with a Fenwick tree answers every query assigned to that prefix without storing the quadratic DP table.¶
prefix_substring_lcs.hpp¶
Answer LCS lengths between prefixes of one sequence and substrings of another. The dynamic-programming grid is represented by its seaweed permutation: after adding one symbol of the first sequence, a single scan swaps boundary endpoints exactly where equality or crossing changes the next LCS row. For a fixed prefix, the LCS deficit of [left, right) equals the number of active endpoints entering that interval from before left. Sweeping right and counting endpoint positions with a Fenwick tree answers every query assigned to that prefix without storing the quadratic DP table.
Verified by prefix_substring_lcs.
对固定串的每个前缀,求它与另一串指定子串的最长公共子序列长度。
Implementation¶
#ifndef NOYA_PREFIX_SUBSTRING_LCS_HPP
#define NOYA_PREFIX_SUBSTRING_LCS_HPP 1
/// @complexity Time: O(m n log n + q). Space: O(n + q), for sequence
/// lengths m, n and q offline queries.
#include "atcoder/fenwicktree.hpp"
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
namespace noya {
struct prefix_substring_lcs_query {
int prefix;
int left;
int right;
};
/// @brief Answer LCS lengths between prefixes of one sequence and substrings
/// of another. The dynamic-programming grid is represented by its seaweed
/// permutation: after adding one symbol of the first sequence, a single scan
/// swaps boundary endpoints exactly where equality or crossing changes the
/// next LCS row. For a fixed prefix, the LCS deficit of [left, right) equals
/// the number of active endpoints entering that interval from before left.
/// Sweeping right and counting endpoint positions with a Fenwick tree answers
/// every query assigned to that prefix without storing the quadratic DP table.
template <class FirstSequence, class SecondSequence>
std::vector<int>
prefix_substring_lcs(const FirstSequence &first, const SecondSequence &second,
const std::vector<prefix_substring_lcs_query> &queries) {
int first_size = int(first.size());
int second_size = int(second.size());
int query_count = int(queries.size());
std::vector<int> answer(query_count);
std::vector<std::vector<int>> by_prefix(first_size);
for (int id = 0; id < query_count; id++) {
auto [prefix, left, right] = queries[id];
assert(0 <= prefix && prefix <= first_size);
assert(0 <= left && left <= right && right <= second_size);
if (prefix != 0 && left != right) {
by_prefix[prefix - 1].push_back(id);
}
}
std::vector<int> endpoint(second_size);
std::iota(endpoint.begin(), endpoint.end(), 0);
std::vector<std::vector<int>> by_right(second_size);
for (int prefix_end = 0; prefix_end < first_size; prefix_end++) {
int displaced = -1;
for (int position = 0; position < second_size; position++) {
if (first[prefix_end] == second[position] ||
endpoint[position] < displaced) {
std::swap(endpoint[position], displaced);
}
}
for (int id : by_prefix[prefix_end]) {
by_right[queries[id].right - 1].push_back(id);
}
atcoder::fenwick_tree<int> active(second_size);
int active_count = 0;
for (int right = 0; right < second_size; right++) {
if (endpoint[right] != -1) {
active.add(endpoint[right], 1);
active_count++;
}
for (int id : by_right[right]) {
int left = queries[id].left;
int length = queries[id].right - left;
answer[id] = length - (active_count - active.sum(0, left));
}
by_right[right].clear();
}
}
return answer;
}
} // namespace noya
#endif // NOYA_PREFIX_SUBSTRING_LCS_HPP
#include <algorithm>
#include <cassert>
#include <numeric>
#include <type_traits>
#include <vector>
/// @complexity Time: O(m n log n + q). Space: O(n + q), for sequence
/// lengths m, n and q offline queries.
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
namespace atcoder {
// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
using U = internal::to_unsigned_t<T>;
public:
fenwick_tree() : _n(0) {}
explicit fenwick_tree(int n) : _n(n), data(n) {}
void add(int p, T x) {
assert(0 <= p && p < _n);
p++;
while (p <= _n) {
data[p - 1] += U(x);
p += p & -p;
}
}
T sum(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
return sum(r) - sum(l);
}
private:
int _n;
std::vector<U> data;
U sum(int r) {
U s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
} // namespace atcoder
namespace noya {
struct prefix_substring_lcs_query {
int prefix;
int left;
int right;
};
/// @brief Answer LCS lengths between prefixes of one sequence and substrings
/// of another. The dynamic-programming grid is represented by its seaweed
/// permutation: after adding one symbol of the first sequence, a single scan
/// swaps boundary endpoints exactly where equality or crossing changes the
/// next LCS row. For a fixed prefix, the LCS deficit of [left, right) equals
/// the number of active endpoints entering that interval from before left.
/// Sweeping right and counting endpoint positions with a Fenwick tree answers
/// every query assigned to that prefix without storing the quadratic DP table.
template <class FirstSequence, class SecondSequence>
std::vector<int>
prefix_substring_lcs(const FirstSequence &first, const SecondSequence &second,
const std::vector<prefix_substring_lcs_query> &queries) {
int first_size = int(first.size());
int second_size = int(second.size());
int query_count = int(queries.size());
std::vector<int> answer(query_count);
std::vector<std::vector<int>> by_prefix(first_size);
for (int id = 0; id < query_count; id++) {
auto [prefix, left, right] = queries[id];
assert(0 <= prefix && prefix <= first_size);
assert(0 <= left && left <= right && right <= second_size);
if (prefix != 0 && left != right) {
by_prefix[prefix - 1].push_back(id);
}
}
std::vector<int> endpoint(second_size);
std::iota(endpoint.begin(), endpoint.end(), 0);
std::vector<std::vector<int>> by_right(second_size);
for (int prefix_end = 0; prefix_end < first_size; prefix_end++) {
int displaced = -1;
for (int position = 0; position < second_size; position++) {
if (first[prefix_end] == second[position] ||
endpoint[position] < displaced) {
std::swap(endpoint[position], displaced);
}
}
for (int id : by_prefix[prefix_end]) {
by_right[queries[id].right - 1].push_back(id);
}
atcoder::fenwick_tree<int> active(second_size);
int active_count = 0;
for (int right = 0; right < second_size; right++) {
if (endpoint[right] != -1) {
active.add(endpoint[right], 1);
active_count++;
}
for (int id : by_right[right]) {
int left = queries[id].left;
int length = queries[id].right - left;
answer[id] = length - (active_count - active.sum(0, left));
}
by_right[right].clear();
}
}
return answer;
}
} // namespace noya