sirkit-cpp-library

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

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

文字列の最小・最大巡回シフト

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

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

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

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

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

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

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

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

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

文字列を任意の位置で切り、後半、前半の順に並べた文字列を巡回シフトと呼びます。その中で辞書順が最小または最大になる開始位置を求めます。

min_cyclic_rotation_index

std::size_t min_cyclic_rotation_index(std::string_view text) noexcept

辞書順で最小の巡回シフトの開始位置を返します。開始位置は0始まりです。結果が同じなら最も小さい開始位置を選び、空文字列では0を返します。比較には符号なしバイトの値を使います。

長さを $n$ として $O(n+1)$ 時間、追加領域は $O(1)$ です。

max_cyclic_rotation_index

std::size_t max_cyclic_rotation_index(std::string_view text) noexcept

辞書順で最大の巡回シフトの開始位置を返します。添字、同順位、空文字列の扱いと計算量は最小版と共通です。

使用例

std::string text = "aaba";
const auto start = sirkit::min_cyclic_rotation_index(text); // 3
const auto rotated = text.substr(start) + text.substr(0, start); // "aaab"

実装・使用例の全体

ソースのAPI注記
min_cyclic_rotation_index(text): 辞書順で最小の巡回シフトの開始位置を返す。
max_cyclic_rotation_index(text): 辞書順で最大の巡回シフトの開始位置を返す。
文字は符号なしバイトとして比較。同順位なら最小の開始位置、空文字なら0。各O(n+1)時間・O(1)追加領域。
Verifyプログラムと検証結果

Verifyプログラム

通常版の検証結果です。

問題 プログラム 現行ソースの状態 提出結果
String Shifting cyclic_rotation.cpp 未提出 —

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

ローカル・CI検査

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

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

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

Verified with

Code

// SPDX-License-Identifier: CC0-1.0
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/cyclic_rotation.md
//
// API一覧
// min_cyclic_rotation_index(text): 辞書順で最小の巡回シフトの開始位置を返す。
// max_cyclic_rotation_index(text): 辞書順で最大の巡回シフトの開始位置を返す。
// 文字は符号なしバイトとして比較。同順位なら最小の開始位置、空文字なら0。各O(n+1)時間・O(1)追加領域。

// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <cstddef>
#include <string_view>

// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_CYCLIC_ROTATION_DEFINED
#define SIRKIT_CYCLIC_ROTATION_DEFINED

namespace detail
{
inline std::size_t cyclic_rotation_offset(std::size_t start, std::size_t offset, std::size_t length) noexcept
{
    const std::size_t tail = length - start;
    return offset < tail ? start + offset : offset - tail;
}

template<bool Maximize>
std::size_t cyclic_rotation_index(std::string_view text) noexcept
{
    const std::size_t length = text.size();
    std::size_t first = 0;
    std::size_t second = 1;
    std::size_t matched = 0;
    while (first < length && second < length && matched < length)
    {
        const auto a = static_cast<unsigned char>(text[cyclic_rotation_offset(first, matched, length)]);
        const auto b = static_cast<unsigned char>(text[cyclic_rotation_offset(second, matched, length)]);
        if (a == b)
        {
            ++matched;
            continue;
        }

        const bool first_loses = Maximize ? a < b : a > b;
        auto &loser = first_loses ? first : second;
        const auto winner = first_loses ? second : first;
        // 一致部分をずらしても最初の大小関係は同じなので、負けた候補の先頭から一致長までを除く。
        loser += std::min(matched + 1, length - loser);
        if (loser == winner)
        {
            ++loser;
        }
        matched = 0;
    }
    return std::min(first, second);
}
} // namespace detail

inline std::size_t min_cyclic_rotation_index(std::string_view text) noexcept
{
    return detail::cyclic_rotation_index<false>(text);
}

inline std::size_t max_cyclic_rotation_index(std::string_view text) noexcept
{
    return detail::cyclic_rotation_index<true>(text);
}

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