競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
文字列を連続した回文に分割し、その個数を最小にします。
const auto cuts = sirkit::min_palindromic_partition("noonabbad");
// cuts = {0, 4, 8, 9}。分割は "noon", "abba", "d"。
min_palindromic_partitionstd::vector<int> min_palindromic_partition(std::string_view text);
std::string_view を受け取り、最少個数で分割した境界の列を std::vector<int> で返します。各部分は [cuts[i], cuts[i+1])、個数は cuts.size() - 1 です。空文字列の結果は {0}。最適な分割が複数あれば、そのうち一つを返します。
NUL を含む任意のバイトを扱い、境界もバイト単位です。長さが INT_MAX - 1 を超えると std::length_error を送出します。
回文木で等差数列をなす長さの接尾回文をまとめ、接頭辞ごとの最小個数と直前の境界を求めます。長さ $N$ に対して $O(N\log(N+1))$ 時間、$O(N)$ 領域です。
min_palindromic_partition(text): 非空の回文に最少個数で分割し、0とNを含む境界列を返す。
空列は{0}。同率ならいずれか。任意のバイトを扱い、添字はバイト位置。
O(N log(N+1))時間、O(N)領域。N>INT_MAX-1ならlength_error。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| 回文の最小分割 | palindromic_factorization_local.cpp | ローカル補足 | — |
| 回文の最小分割の独立参照テスト | palindromic_factorization_reference.cpp | ローカル補足 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考: M. Rubinchik, A. M. Shur, EERTREE: An Efficient Data Structure for Processing Palindromes in Strings, §4.1
// (https://arxiv.org/abs/1506.04862) の series link と最小分割DP。コードは独自実装。
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/palindromic_factorization.md
//
// API一覧
// min_palindromic_partition(text): 非空の回文に最少個数で分割し、0とNを含む境界列を返す。
// 空列は{0}。同率ならいずれか。任意のバイトを扱い、添字はバイト位置。
// O(N log(N+1))時間、O(N)領域。N>INT_MAX-1ならlength_error。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <limits>
#include <stdexcept>
#include <string_view>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_PALINDROMIC_FACTORIZATION_DEFINED
#define SIRKIT_PALINDROMIC_FACTORIZATION_DEFINED
namespace detail
{
inline int PalindromicFactorizationSize(std::size_t length)
{
if (length > static_cast<std::size_t>(std::numeric_limits<int>::max() - 1))
{
throw std::length_error("min_palindromic_partition: text is too long");
}
return static_cast<int>(length);
}
} // namespace detail
inline std::vector<int> min_palindromic_partition(std::string_view text)
{
struct Palindrome
{
int length = 0;
int suffix = 0;
int series = 0;
int period = 0;
int first_child = -1;
int next_sibling = -1;
int best_prefix = 0;
char symbol = 0;
};
const int n = detail::PalindromicFactorizationSize(text.size());
constexpr int kOddRoot = 0, kEvenRoot = 1;
std::vector<Palindrome> nodes;
nodes.reserve(text.size() + 2);
nodes.push_back({.length = -1});
nodes.emplace_back();
std::vector<int> parts(n + 1), previous(n + 1);
const auto extendable = [&](int node, int position)
{
for (;; node = nodes[node].suffix)
{
const int before = position - nodes[node].length - 1;
if (before >= 0 && text[before] == text[position])
{
return node;
}
}
};
const auto transition = [&](int node, char symbol)
{
for (int child = nodes[node].first_child; child >= 0; child = nodes[child].next_sibling)
{
if (nodes[child].symbol == symbol)
{
return child;
}
}
return -1;
};
int longest_suffix = kEvenRoot;
for (int end = 1; end <= n; ++end)
{
const char symbol = text[end - 1];
const int inside = extendable(longest_suffix, end - 1);
longest_suffix = transition(inside, symbol);
if (longest_suffix < 0)
{
const int length = nodes[inside].length + 2;
const int suffix = inside == kOddRoot
? kEvenRoot
: transition(extendable(nodes[inside].suffix, end - 1), symbol);
const int period = length - nodes[suffix].length;
const int series = period == nodes[suffix].period ? nodes[suffix].series : suffix;
longest_suffix = static_cast<int>(nodes.size());
nodes.push_back({.length = length,
.suffix = suffix,
.series = series,
.period = period,
.next_sibling = nodes[inside].first_child,
.symbol = symbol});
nodes[inside].first_child = longest_suffix;
}
int best = end - 1;
for (int node = longest_suffix; nodes[node].length > 0; node = nodes[node].series)
{
auto &palindrome = nodes[node];
int prefix = end - (nodes[palindrome.series].length + palindrome.period);
const auto &suffix = nodes[palindrome.suffix];
// 等差列の最短の回文だけを新たに試す。残りの回文の開始位置は
// period 個前の接頭辞で処理した suffix の系列と一致する。
if (palindrome.period == suffix.period && parts[suffix.best_prefix] < parts[prefix])
{
prefix = suffix.best_prefix;
}
palindrome.best_prefix = prefix;
if (parts[prefix] < parts[best])
{
best = prefix;
}
}
parts[end] = parts[best] + 1;
previous[end] = best;
}
std::vector<int> cuts;
cuts.reserve(parts[n] + 1);
for (int end = n; end > 0; end = previous[end])
{
cuts.push_back(end);
}
cuts.push_back(0);
std::ranges::reverse(cuts);
return cuts;
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1