競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
元の文字列を何回も連結した列に、目標の各文字を同じ回数ずつ増やした部分列があるか調べます。繰り返した文字列は作りません。
RepeatedSubsequenceIndexexplicit RepeatedSubsequenceIndex(std::string_view cycle);
長さ $n$ の cycle をバイト単位で前処理します。$O(n+256)$ 時間・領域を使います。コピーした索引は独立し、移動元は空です。
contains_expandedbool contains_expanded(std::uint64_t copies, std::string_view target,
std::uint64_t repetitions) const;
cycle を copies 回連結した列の中に、target の各バイトをその場で repetitions 個ずつに増やした部分列があれば true を返します。例えば target = "ab" と repetitions = 2 なら、探す部分列は "aabb" です。目標が空、または repetitions が0なら常に成立します。目標の長さを $m$ として $O(1+m\log(n+2))$ 時間です。
max_repetition成立する最大の repetitions を limit 以下から求めます。limit の既定値は UINT64_MAX です。目標が空なら limit を返します。$O(1+m\log(n+2)\log(\mathrm{limit}+2))$ 時間です。
const sirkit::RepeatedSubsequenceIndex index("abc");
assert(index.contains_expanded(3, "ab", 2)); // abcabcabc に aabb がある
assert(index.max_repetition(3, "ab") == 2);
RepeatedSubsequenceIndex(cycle): 各バイトの出現位置を記録。O(n+256)時間・領域、nはcycleの長さ。コピーは独立し、移動元は空。 contains_expanded(copies,target,repetitions): cycleをcopies回並べた列に、targetの各バイトをその場でrepetitions個に増やした部分列があるか。 max_repetition(copies,target,limit): 条件を満たす0以上limit以下の最大repetitions。limit既定はUINT64_MAX、空targetはlimit。 空列・0回の照会可。回数はuint64_t。繰り返した文字列を実体化しない。 mをtargetの長さとし、contains_expandedはO(1+m*log(n+2))、max_repetitionはO(1+m*log(n+2)*log(limit+2))時間。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| SSttrriinngg in StringString | repeated_subsequence.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(周期列での照会): https://atcoder.jp/contests/abc346/tasks/abc346_f
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/repeated_subsequence.md
//
// API一覧
// RepeatedSubsequenceIndex(cycle): 各バイトの出現位置を記録。O(n+256)時間・領域、nはcycleの長さ。コピーは独立し、移動元は空。
// contains_expanded(copies,target,repetitions): cycleをcopies回並べた列に、targetの各バイトをその場でrepetitions個に増やした部分列があるか。
// max_repetition(copies,target,limit): 条件を満たす0以上limit以下の最大repetitions。limit既定はUINT64_MAX、空targetはlimit。
// 空列・0回の照会可。回数はuint64_t。繰り返した文字列を実体化しない。
// mをtargetの長さとし、contains_expandedはO(1+m*log(n+2))、max_repetitionはO(1+m*log(n+2)*log(limit+2))時間。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <string_view>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_REPEATED_SUBSEQUENCE_DEFINED
#define SIRKIT_REPEATED_SUBSEQUENCE_DEFINED
class RepeatedSubsequenceIndex
{
using Count = std::uint64_t;
std::array<std::vector<std::size_t>, 256> positions_;
public:
explicit RepeatedSubsequenceIndex(std::string_view cycle)
{
for (std::size_t i = 0; i < cycle.size(); ++i)
{
positions_[static_cast<unsigned char>(cycle[i])].push_back(i);
}
}
RepeatedSubsequenceIndex(const RepeatedSubsequenceIndex &) = default;
RepeatedSubsequenceIndex(RepeatedSubsequenceIndex &&other) noexcept
{
positions_.swap(other.positions_);
}
RepeatedSubsequenceIndex &operator=(RepeatedSubsequenceIndex other) noexcept
{
positions_.swap(other.positions_);
return *this;
}
[[nodiscard]] bool contains_expanded(Count copies, std::string_view target, Count repetitions) const
{
if (repetitions == 0 || target.empty())
{
return true;
}
Count copy = 0;
std::size_t offset = 0;
for (const char byte : target)
{
const auto &positions = positions_[static_cast<unsigned char>(byte)];
if (positions.empty())
{
return false;
}
const Count frequency = positions.size();
const Count start = static_cast<Count>(std::lower_bound(positions.begin(), positions.end(), offset) - positions.begin());
Count advance = (repetitions - 1) / frequency;
const Count remainder = (repetitions - 1) % frequency;
const Count available = frequency - start;
Count position;
// 周期内の出現番号と周期を分け、回数と文字列長の積を取らない。
if (remainder >= available)
{
++advance;
position = remainder - available;
}
else
{
position = start + remainder;
}
if (advance >= copies - copy)
{
return false;
}
copy += advance;
offset = positions[static_cast<std::size_t>(position)] + 1;
}
return true;
}
[[nodiscard]] Count max_repetition(Count copies, std::string_view target,
Count limit = std::numeric_limits<Count>::max()) const
{
if (target.empty())
{
return limit;
}
if (copies == 0 || limit == 0)
{
return 0;
}
std::array<Count, 256> needed{};
for (const char byte : target)
{
++needed[static_cast<unsigned char>(byte)];
}
Count high = limit;
for (std::size_t byte = 0; byte < needed.size(); ++byte)
{
if (needed[byte] == 0)
{
continue;
}
const Count frequency = positions_[byte].size();
if (frequency <= std::numeric_limits<Count>::max() / copies)
{
high = std::min(high, frequency * copies / needed[byte]);
}
}
if (contains_expanded(copies, target, high))
{
return high;
}
--high;
Count low = 0;
while (low < high)
{
const Count distance = high - low;
const Count mid = low + distance / 2 + distance % 2;
if (contains_expanded(copies, target, mid))
{
low = mid;
}
else
{
high = mid - 1;
}
}
return low;
}
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1