競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
文字列の長さを固定したまま、1バイトの置換とパターン検索を繰り返せます。同じパターンが重なって現れる場合も、開始位置ごとに区別します。
DynamicSubstringMatchingexplicit DynamicSubstringMatching(std::string_view text)
入力のコピーと各バイトの出現位置を保持します。入力長を $n$ として、構築時間と使用領域は $O(n+1)$ です。コピーを更新しても元には影響せず、移動元は空になります。
sizestd::size_t size() const noexcept
元のバイト列の長さを $O(1)$ 時間で返します。
setvoid set(std::size_t position, char replacement)
0始まりの position にあるバイトを $O(1)$ 時間で置き換えます。position < size() が前提です。
containsbool contains(std::string_view pattern) const
pattern と完全一致する連続部分文字列があれば true です。
countstd::size_t count(std::string_view pattern) const
pattern が現れる回数を返します。
occurrencesstd::vector<std::size_t> occurrences(std::string_view pattern) const
一致した0始まりの開始位置を昇順で返します。空パターンは末尾を含む $n+1$ 箇所に一致します。元の列より長いパターンに一致する位置はありません。
検索は開始位置の候補を64個ずつ絞り込みます。パターン長 $m$ が $1\le m\le n$ なら、$O(m(1+\lceil(n-m+1)/64\rceil))$ 時間・$O(m)$ 追加領域です。空パターンの存在判定と個数は $O(1)$ 時間で求まり、位置を返す場合は結果の個数に比例する時間と領域が加わります。
sirkit::DynamicSubstringMatching text("ababa");
assert(text.count("aba") == 2);
text.set(2, 'x');
assert(!text.contains("aba"));
DynamicSubstringMatching(text): 固定長バイト列を所有し、各バイトの出現位置を64ビットずつ記録する。 size(): 元のバイト列の長さ。O(1)。 set(position, byte): 0始まりの位置を1バイト置換する。O(1)。 contains(pattern): patternが連続部分文字列として現れるかを返す。 count(pattern): 重なりも含めた出現回数を返す。 occurrences(pattern): 出現する0始まりの位置を昇順で返す。 空patternはn+1箇所に現れ、長すぎるpatternは0箇所。nは元の長さ、mはパターン長。 構築・保管O(n+1)。空でないm<=nの検索はO(m*(1+ceil((n-m+1)/64)))時間・O(m)追加領域。出力にはさらに結果の領域と時間が必要。 コピーは独立。移動元は空として使える。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Itsuki | dynamic_substring_matching.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/dynamic_substring_matching.md
//
// API一覧
// DynamicSubstringMatching(text): 固定長バイト列を所有し、各バイトの出現位置を64ビットずつ記録する。
// size(): 元のバイト列の長さ。O(1)。
// set(position, byte): 0始まりの位置を1バイト置換する。O(1)。
// contains(pattern): patternが連続部分文字列として現れるかを返す。
// count(pattern): 重なりも含めた出現回数を返す。
// occurrences(pattern): 出現する0始まりの位置を昇順で返す。
// 空patternはn+1箇所に現れ、長すぎるpatternは0箇所。nは元の長さ、mはパターン長。
// 構築・保管O(n+1)。空でないm<=nの検索はO(m*(1+ceil((n-m+1)/64)))時間・O(m)追加領域。出力にはさらに結果の領域と時間が必要。
// コピーは独立。移動元は空として使える。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_DYNAMIC_SUBSTRING_MATCHING_DEFINED
#define SIRKIT_DYNAMIC_SUBSTRING_MATCHING_DEFINED
class DynamicSubstringMatching
{
public:
explicit DynamicSubstringMatching(std::string_view text) : text_(text)
{
const auto blocks = text.size() / kWordBits + (text.size() % kWordBits != 0);
// 右にずらした窓を取り出すとき、末尾からも次の語を読めるようにする。
for (auto &row : positions_)
{
row.resize(blocks + 1);
}
for (std::size_t i = 0; i < text.size(); ++i)
{
const auto byte = static_cast<unsigned char>(text[i]);
++frequency_[byte];
positions_[byte][i / kWordBits] |= bit_at(i);
}
}
DynamicSubstringMatching(const DynamicSubstringMatching &) = default;
DynamicSubstringMatching(DynamicSubstringMatching &&other) noexcept
{
swap_state(other);
}
DynamicSubstringMatching &operator=(DynamicSubstringMatching other) noexcept
{
swap_state(other);
return *this;
}
[[nodiscard]] std::size_t size() const noexcept
{
return text_.size();
}
void set(std::size_t position, char replacement)
{
#ifndef SKIP_RUNTIME_CHECKS
if (position >= size())
{
throw std::out_of_range("DynamicSubstringMatching: position is out of range");
}
#endif
const auto old = static_cast<unsigned char>(text_[position]);
const auto next = static_cast<unsigned char>(replacement);
if (old == next)
{
return;
}
const auto index = position / kWordBits;
const auto bit = bit_at(position);
positions_[old][index] &= ~bit;
positions_[next][index] |= bit;
--frequency_[old];
++frequency_[next];
text_[position] = replacement;
}
[[nodiscard]] bool contains(std::string_view pattern) const
{
return pattern.empty() || scan(pattern, [](std::size_t, std::uint64_t) { return true; });
}
[[nodiscard]] std::size_t count(std::string_view pattern) const
{
if (pattern.empty())
{
return size() + 1;
}
std::size_t result = 0;
scan(pattern, [&](std::size_t, std::uint64_t candidates) {
result += static_cast<std::size_t>(std::popcount(candidates));
return false;
});
return result;
}
[[nodiscard]] std::vector<std::size_t> occurrences(std::string_view pattern) const
{
if (pattern.empty())
{
std::vector<std::size_t> all(size() + 1);
std::iota(all.begin(), all.end(), std::size_t{0});
return all;
}
std::vector<std::size_t> result;
scan(pattern, [&](std::size_t base, std::uint64_t candidates) {
do
{
result.push_back(base + static_cast<std::size_t>(std::countr_zero(candidates)));
candidates &= candidates - 1;
} while (candidates != 0);
return false;
});
return result;
}
private:
static constexpr unsigned kWordBits = 64;
static constexpr unsigned kAlphabetSize = 256;
void swap_state(DynamicSubstringMatching &other) noexcept
{
text_.swap(other.text_);
positions_.swap(other.positions_);
frequency_.swap(other.frequency_);
}
static constexpr std::uint64_t bit_at(std::size_t position) noexcept
{
return std::uint64_t{1} << (position % kWordBits);
}
struct Constraint
{
const std::uint64_t *words;
unsigned shift;
[[nodiscard]] std::uint64_t at(std::size_t block) const noexcept
{
const auto word = words[block];
if (shift == 0)
{
return word;
}
return (word >> shift) | (words[block + 1] << (kWordBits - shift));
}
};
template<class Consume>
bool scan(std::string_view pattern, Consume consume) const
{
if (pattern.size() > size())
{
return false;
}
std::vector<Constraint> constraints;
constraints.reserve(pattern.size());
std::size_t rarest = 0;
auto smallest_frequency = size();
for (std::size_t i = 0; i < pattern.size(); ++i)
{
const auto byte = static_cast<unsigned char>(pattern[i]);
if (frequency_[byte] == 0)
{
return false;
}
if (frequency_[byte] < smallest_frequency)
{
rarest = i;
smallest_frequency = frequency_[byte];
}
constraints.push_back({positions_[byte].data() + i / kWordBits, static_cast<unsigned>(i % kWordBits)});
}
std::swap(constraints.front(), constraints[rarest]);
const auto last_start = size() - pattern.size();
const auto last_block = last_start / kWordBits;
const auto tail_mask = ~std::uint64_t{0} >> ((kWordBits - 1) - last_start % kWordBits);
for (std::size_t block = 0; block <= last_block; ++block)
{
auto candidates = block == last_block ? tail_mask : ~std::uint64_t{0};
for (const auto &constraint : constraints)
{
candidates &= constraint.at(block);
if (candidates == 0)
{
break;
}
}
if (candidates != 0 && consume(block * kWordBits, candidates))
{
return true;
}
}
return false;
}
std::string text_;
std::array<std::vector<std::uint64_t>, kAlphabetSize> positions_;
std::array<std::size_t, kAlphabetSize> frequency_{};
};
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1