競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
文字列の各接頭辞について、周期や接頭辞と接尾辞の一致を調べます。たとえば abababa は ab を繰り返す途中で作れますが、ab を整数回だけ繰り返して作ることはできません。
StringPeriodsexplicit StringPeriods(std::string_view text)
文字列をバイト列として前処理します。構築後は入力文字列の保存が不要です。長さを $n$ として $O(n+1)$ 時間・領域です。
sizestd::size_t size() const noexcept
元の文字列のバイト数を $O(1)$ 時間で返します。
以下の関数は先頭 length バイトを対象とし、length <= size() が前提です。
least_periodstd::size_t least_period(std::size_t length) const
すべての重なる位置で text[i] == text[i + p] が成り立つ最小の正の p を $O(1)$ 時間で返します。繰り返しの途中で終わる場合も周期とし、空の接頭辞では0を返します。
primitive_root_lengthstd::size_t primitive_root_length(std::size_t length) const
整数回の繰り返しで接頭辞全体を作れる最短のブロック長を $O(1)$ 時間で返します。1回でしか作れなければ length、空なら0を返します。
proper_bordersstd::vector<std::size_t> proper_borders(std::size_t length) const
接頭辞かつ接尾辞になっている文字列の長さを降順で返します。0と length は含みません。返す個数を $k$ として $O(k+1)$ 時間・領域です。
periodsstd::vector<std::size_t> periods(std::size_t length) const
正の周期を昇順で返します。空なら空の配列、非空なら length も含めます。返す個数を $k$ として $O(k+1)$ 時間・領域です。
const sirkit::StringPeriods index("abababa");
assert(index.least_period(7) == 2);
assert(index.primitive_root_length(7) == 7);
assert(index.primitive_root_length(6) == 2);
// index.periods(7) は {2, 4, 6, 7}。
StringPeriods(text): 各接頭辞の周期を答えるためにO(n+1)時間・領域で前処理。 size(): 元の文字列のバイト数。O(1)。 least_period(length): 先頭lengthバイトの最小周期。末尾は繰り返しの途中で終わってよい。O(1)。 primitive_root_length(length): 先頭lengthバイトを完全な繰り返しで作る最短のブロック長。O(1)。 proper_borders(length): 先頭lengthバイトの真の接頭辞かつ接尾辞の長さを降順で返す。長さ0は除く。 periods(length): 先頭lengthバイトの正の周期を昇順で返す。length自体を含む。 列挙はO(k+1)時間・領域で、kは返す要素数。空の接頭辞の最短長は0、列挙結果は空。 lengthはsize()以下。 [同梱のPrefix Function] prefix_function(text): 各接頭辞の真の接頭辞かつ接尾辞の最大長。O(n)時間・領域。 find_occurrences(text,pattern): 一致の開始位置を昇順で返す。重複する出現も含む。 空のpatternは境界0からtext.size()までに一致。O(n+m)時間・O(m+k)領域、kは一致数。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Best Representation | string_periods_best_representation.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/string_periods.md
//
// API一覧
// StringPeriods(text): 各接頭辞の周期を答えるためにO(n+1)時間・領域で前処理。
// size(): 元の文字列のバイト数。O(1)。
// least_period(length): 先頭lengthバイトの最小周期。末尾は繰り返しの途中で終わってよい。O(1)。
// primitive_root_length(length): 先頭lengthバイトを完全な繰り返しで作る最短のブロック長。O(1)。
// proper_borders(length): 先頭lengthバイトの真の接頭辞かつ接尾辞の長さを降順で返す。長さ0は除く。
// periods(length): 先頭lengthバイトの正の周期を昇順で返す。length自体を含む。
// 列挙はO(k+1)時間・領域で、kは返す要素数。空の接頭辞の最短長は0、列挙結果は空。
// lengthはsize()以下。
//
// [同梱のPrefix Function]
// prefix_function(text): 各接頭辞の真の接頭辞かつ接尾辞の最大長。O(n)時間・領域。
// find_occurrences(text,pattern): 一致の開始位置を昇順で返す。重複する出現も含む。
// 空のpatternは境界0からtext.size()までに一致。O(n+m)時間・O(m+k)領域、kは一致数。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <cstddef>
#include <numeric>
#include <stdexcept>
#include <string_view>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_PREFIX_FUNCTION_DEFINED
#define SIRKIT_PREFIX_FUNCTION_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
inline std::size_t occurrence_boundary_count(std::size_t size, std::size_t capacity)
{
if (size >= capacity)
{
throw std::length_error("find_occurrences: too many occurrences");
}
return size + 1;
}
} // namespace detail
inline std::vector<std::size_t> prefix_function(std::string_view text)
{
std::vector<std::size_t> length(text.size());
for (std::size_t i = 1; i < text.size(); ++i)
{
auto previous = length[i - 1];
while (previous != 0 && text[i] != text[previous])
{
previous = length[previous - 1];
}
length[i] = previous + (text[i] == text[previous]);
}
return length;
}
inline std::vector<std::size_t> find_occurrences(std::string_view text, std::string_view pattern)
{
std::vector<std::size_t> result;
if (pattern.empty())
{
result.resize(detail::occurrence_boundary_count(text.size(), result.max_size()));
std::iota(result.begin(), result.end(), std::size_t{0});
return result;
}
const auto border = prefix_function(pattern);
std::size_t matched = 0;
for (std::size_t i = 0; i < text.size(); ++i)
{
while (matched != 0 && text[i] != pattern[matched])
{
matched = border[matched - 1];
}
matched += text[i] == pattern[matched];
if (matched == pattern.size())
{
result.push_back(i - (matched - 1));
matched = border[matched - 1];
}
}
return result;
}
#endif
#ifndef SIRKIT_STRING_PERIODS_DEFINED
#define SIRKIT_STRING_PERIODS_DEFINED
class StringPeriods
{
public:
explicit StringPeriods(std::string_view text) : border_(prefix_function(text))
{
}
[[nodiscard]] std::size_t size() const noexcept
{
return border_.size();
}
[[nodiscard]] std::size_t least_period(std::size_t length) const
{
return length - last_border(length);
}
[[nodiscard]] std::size_t primitive_root_length(std::size_t length) const
{
const auto period = least_period(length);
if (period == 0 || length % period == 0)
{
return period;
}
return length;
}
[[nodiscard]] std::vector<std::size_t> proper_borders(std::size_t length) const
{
std::vector<std::size_t> result;
for (auto border = last_border(length); border != 0; border = border_[border - 1])
{
result.push_back(border);
}
return result;
}
[[nodiscard]] std::vector<std::size_t> periods(std::size_t length) const
{
auto result = proper_borders(length);
for (auto &border : result)
{
border = length - border;
}
if (length != 0)
{
result.push_back(length);
}
return result;
}
private:
std::vector<std::size_t> border_;
[[nodiscard]] std::size_t last_border(std::size_t length) const
{
#ifndef SKIP_RUNTIME_CHECKS
if (length > border_.size())
{
throw std::out_of_range("StringPeriods: prefix length");
}
#endif
return length == 0 ? 0 : border_[length - 1];
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1