# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
396513 | KoD | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 266 ms | 708 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
class rep {
struct Iter {
usize itr;
constexpr Iter(const usize pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept {
return itr != other.itr;
}
constexpr usize operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr rep(const usize first, const usize last) noexcept
: first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
template <class T> bool setmax(T& lhs, const T& rhs) {
if (lhs < rhs) {
lhs = rhs;
return true;
}
return false;
}
template <class T> using Vec = std::vector<T>;
Vec<usize> knuth_morris_pratt(const std::string& S) {
const auto N = S.size();
Vec<usize> ret(N + 1);
ret[0] = -1;
usize j = -1;
for (const auto i : rep(0, N)) {
while ((~j) and S[i] != S[j]) {
j = ret[j];
}
j += 1;
ret[i + 1] = j;
}
ret[0] = 0;
return ret;
}
std::tuple<usize, usize, usize> solve(const std::string& S,
const std::string& T,
const std::string& revT) {
const auto N = S.size();
const auto M = T.size();
std::tuple<usize, usize, usize> ret(0, 0, 0);
for (const auto split : rep(0, N + 1)) {
const auto s1 = S.substr(0, split);
const auto s2 = S.substr(split, N - split);
const auto A = knuth_morris_pratt(std::string(s1.rbegin(), s1.rend()) +
'$' + revT);
const auto B = knuth_morris_pratt(s2 + '$' + T);
for (const auto k : rep(0, M + 1)) {
const auto f = A[A.size() - k - 1];
const auto b = B[s2.size() + k + 1];
setmax(ret, std::make_tuple(b + f, split - f, k - b));
}
}
return ret;
}
void BOI19_Necklace_main() {
std::string S, T;
std::cin >> S >> T;
const auto revT = std::string(T.rbegin(), T.rend());
const auto [a1, i1, j1] = solve(S, T, revT);
const auto [a2, i2, j2] = solve(S, revT, T);
if (a1 > a2) {
std::cout << a1 << '\n';
std::cout << i1 << ' ' << j1 << '\n';
} else {
std::cout << a2 << '\n';
std::cout << i2 << ' ' << T.size() - (a2 + j2) << '\n';
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
BOI19_Necklace_main();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |