sirkit-cpp-library

競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。

View the Project on GitHub reinsirk/sirkit-cpp-library

周期文字列の中でパターンを繰り返す

String/Periodic_Pattern_Runs.cpp · ソース · 文書 Last Commit

通常版・コンパイラ共通 · この版の一覧へ

通常版・GCC専用 · この版の一覧へ

通常版・Clang専用 · この版の一覧へ

通常版・MSVC専用 · この版の一覧へ

速度優先版・コンパイラ共通 · この版の一覧へ

速度優先版・GCC専用 · この版の一覧へ

速度優先版・Clang専用 · この版の一覧へ

速度優先版・MSVC専用 · この版の一覧へ

period を無限に連ねた列の中に、同じ pattern を隙間なく何回続けて置けるか求めます。

PeriodicPatternRun

start は周期内の開始位置で、0始まりです。repetitions は最大回数を表し、上限がなければ std::nullopt になります。同じ回数なら開始位置の最小を返します。

longest_periodic_pattern_run(period, pattern)

1回も置けなければ {0, 0}、pattern が空なら {0, std::nullopt} を返します。比較はバイト単位です。

KMP で一致する開始位置をそろえ、パターン長ずつ進む周期上の経路を調べます。各文字列の長さを $n$, $m$ とすると、$O(1+n+m)$ 時間・領域です。

使用例

const auto finite = sirkit::longest_periodic_pattern_run("abcabab", "ab");
assert(finite.start == 3 && finite.repetitions == 3);
const auto unbounded = sirkit::longest_periodic_pattern_run("abab", "ab");
assert(unbounded.start == 0 && !unbounded.repetitions);

実装・使用例の全体・Verify

ソースのAPI注記
PeriodicPatternRun{start,repetitions}: 周期内の0始まりの開始位置と連続回数。無限なら回数はnullopt。
longest_periodic_pattern_run(period,pattern): periodを繰り返す列にpatternを何個連結して置けるか求める。
同じ最大回数ならstart最小。1回も置けない場合は{0,0}。空patternは{0,nullopt}。
バイト一致。長さn,mに対しO(1+n+m)時間・領域。

[同梱の接頭辞関数]
prefix_function(text): 各接頭辞の真の接頭辞かつ接尾辞の最大長。O(n)時間・領域。
find_occurrences(text,pattern): 一致の開始位置を昇順で返す。重複する出現も含む。
空のpatternは境界0からtext.size()までに一致。O(n+m)時間・O(m+k)領域、kは一致数。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
Strings of Eternity periodic_pattern_runs.cpp 未提出 —

✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。

ローカル・CI検査

検査 状態
正常系 未実施
失敗系 未測定
Library Checker公式ケース 無し
性能測定 未実施
ASan/UBSan 未実施
カバレッジ 未実施

到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。

実行条件・件数・測定結果 · 結果JSON

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// 参考(周期列内の反復): https://atcoder.jp/contests/abc135/tasks/abc135_f
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/periodic_pattern_runs.md
//
// API一覧
// PeriodicPatternRun{start,repetitions}: 周期内の0始まりの開始位置と連続回数。無限なら回数はnullopt。
// longest_periodic_pattern_run(period,pattern): periodを繰り返す列にpatternを何個連結して置けるか求める。
// 同じ最大回数ならstart最小。1回も置けない場合は{0,0}。空patternは{0,nullopt}。
// バイト一致。長さn,mに対しO(1+n+m)時間・領域。
//
// [同梱の接頭辞関数]
// 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>
#include <optional>


// 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_PERIODIC_PATTERN_RUNS_DEFINED
#define SIRKIT_PERIODIC_PATTERN_RUNS_DEFINED

struct PeriodicPatternRun
{
    std::size_t start;
    std::optional<std::size_t> repetitions;
};

namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
inline std::vector<unsigned char> FindPeriodicPatternStarts(std::string_view period, std::string_view pattern)
{
    const auto border = prefix_function(pattern);
    std::size_t phase = 0, matched = 0;
    const auto scan_next = [&]()
    {
        const char byte = period[phase];
        if (++phase == period.size())
        {
            phase = 0;
        }
        while (matched != 0 && byte != pattern[matched])
        {
            matched = border[matched - 1];
        }
        matched += byte == pattern[matched];
        if (matched == pattern.size())
        {
            matched = border[matched - 1];
            return true;
        }
        return false;
    };

    // 最初の一致の終端を調べる直前まで読み進める。周期列自体は連結しない。
    for (std::size_t i = 1; i < pattern.size(); ++i)
    {
        (void)scan_next();
    }
    std::vector<unsigned char> starts(period.size());
    for (auto &match : starts)
    {
        match = scan_next();
    }
    return starts;
}
} // namespace reinsirk::detail

[[nodiscard]] inline PeriodicPatternRun longest_periodic_pattern_run(std::string_view period, std::string_view pattern)
{
    if (pattern.empty())
    {
        return {0, std::nullopt};
    }
    if (period.empty())
    {
        return {0, 0};
    }

    const auto starts = /*SIRKIT_NS_REWRITE_V1:R:cmVpbnNpcms6OmRldGFpbA==*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::FindPeriodicPatternStarts(period, pattern);
    const std::size_t n = period.size();
    const std::size_t step = pattern.size() % n;
    const std::size_t wrap = n - step;
    const auto advance = [wrap, step](std::size_t position)
    {
        return position < wrap ? position + step : position - wrap;
    };

    std::size_t best = 0, best_start = 0;
    const std::size_t cycles = std::gcd(n, step);
    for (std::size_t seed = 0; seed < cycles; ++seed)
    {
        // 1回消費すると、周期上の位置はpatternの長さだけずれる。
        std::size_t gap = seed;
        while (starts[gap])
        {
            gap = advance(gap);
            if (gap == seed)
            {
                return {seed, std::nullopt};
            }
        }

        // 一致しない位置を境に一周すれば、周回の境界をまたぐ連続区間も数えられる。
        std::size_t position = gap, length = 0, start = 0;
        do
        {
            position = advance(position);
            if (starts[position])
            {
                if (length == 0)
                {
                    start = position;
                }
                ++length;
            }
            else
            {
                if (length > best || (length == best && start < best_start))
                {
                    best = length;
                    best_start = start;
                }
                length = 0;
            }
        } while (position != gap);
    }
    return {best_start, best};
}

#endif

} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1
Back to top page