競技プログラミング向けのC++23ライブラリ。API、使用例、検証結果を確認できます。
通常版・コンパイラ共通 · この版の一覧へ
通常版・GCC専用 · この版の一覧へ
通常版・Clang専用 · この版の一覧へ
通常版・MSVC専用 · この版の一覧へ
速度優先版・コンパイラ共通 · この版の一覧へ
速度優先版・GCC専用 · この版の一覧へ
速度優先版・Clang専用 · この版の一覧へ
速度優先版・MSVC専用 · この版の一覧へ
文字列を辞書順に並べたときの元の添字を求めます。入力を保持したまま、付随する値もその添字で取り出せます。
string_radix_orderstd::vector<std::size_t> string_radix_order(std::span<const std::string> words,
std::string_view alphabet = {});
std::vector<std::size_t> string_radix_order(std::span<const std::string_view> words,
std::string_view alphabet = {});
辞書順に並べたときの0始まりの添字を返します。同じ文字列は入力の順番を保ちます。片方がもう一方の接頭辞なら、短い方が先です。
alphabet は先に並べたいバイトの列です。指定した順を優先し、残りは符号なし値の小さい順に並べます。空なら全バイトを符号なし値で比較します。検査が有効なら、alphabet 内の重複に対して std::invalid_argument を送出します。
空文字列や値0のバイトも扱います。文字列数を $n$、全体のバイト数を $L$ とすると、8ビットのバイトを使う固定の基数ソートで $O(n+L+1)$ 時間、$O(n+1)$ 追加領域です。
const std::array<std::string_view, 5> words{"ab", "a", "ab", "ba", "b"};
const auto order = sirkit::string_radix_order(words, "ba");
assert((order == std::vector<std::size_t>{4, 3, 1, 0, 2}));
実装・使用例の全体・安定ソートのVerify・独自順のVerify
string_radix_order(words,alphabet={}): 文字列を安定に辞書順で並べる0始まり添字列を返す。
wordsはstringまたはstring_viewの連続列。空文字列と任意の8bitバイトを許す。
alphabetは優先順に列挙した異なるバイト。残りは符号なし値の順で続く。重複は検査有効時にinvalid_argument。
文字列数n、総バイト数L。256バイト固定のMSD基数ソート。O(n+L+1)時間、O(n+1)追加領域。
通常版の検証結果です。
| 問題 | プログラム | 現行ソースの状態 | 提出結果 |
|---|---|---|---|
| Neo-lexicographic Ordering | string_radix_order_alphabet.cpp | 未提出 | — |
| 本棚 | string_radix_order_bookshelf.cpp | 未提出 | — |
✓ 現行ACには、提出本文を照合したACと、利用者が現行版の提出を確認したAtCoderのACを含めます。
| 検査 | 状態 |
|---|---|
| 正常系 | 未実施 |
| 失敗系 | 未測定 |
| Library Checker公式ケース | 無し |
| 性能測定 | 未実施 |
| ASan/UBSan | 未実施 |
| カバレッジ | 未実施 |
到達不能と証明した箇所を除き、通常版本体のカバレッジは行・直接分岐とも100%が合格条件です。未計測の場合も不合格です。
// SPDX-License-Identifier: CC0-1.0
// 参考(Verifyと辞書順の定義): https://atcoder.jp/contests/abc219/tasks/abc219_c
// ドキュメント: https://github.com/reinsirk/sirkit-cpp-library/blob/main/Document/string_radix_order.md
//
// API一覧
// string_radix_order(words,alphabet={}): 文字列を安定に辞書順で並べる0始まり添字列を返す。
// wordsはstringまたはstring_viewの連続列。空文字列と任意の8bitバイトを許す。
// alphabetは優先順に列挙した異なるバイト。残りは符号なし値の順で続く。重複は検査有効時にinvalid_argument。
// 文字列数n、総バイト数L。256バイト固定のMSD基数ソート。O(n+L+1)時間、O(n+1)追加領域。
// SIRKIT_NAMESPACE_V1: 公開APIはsirkit、既存のgeoはsirkit::geo、内部部品はsirkit::detail。
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <numeric>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
// SIRKIT_NAMESPACE_OPEN_V1
namespace sirkit {
#ifndef SIRKIT_STRING_RADIX_ORDER_DEFINED
#define SIRKIT_STRING_RADIX_ORDER_DEFINED
namespace /*SIRKIT_NS_REWRITE_V1:D:cmVpbnNpcms6OmRldGFpbA==*/detail/*SIRKIT_NS_REWRITE_END_V1*/
{
using StringRadixDigit = std::uint16_t;
constexpr std::size_t kStringRadixAlphabetSize = 256;
inline auto MakeStringRadixRanks(std::string_view alphabet)
{
std::array<StringRadixDigit, kStringRadixAlphabetSize> ranks{};
StringRadixDigit next = 1;
for (const unsigned char byte : alphabet)
{
#ifndef SKIP_RUNTIME_CHECKS
if (ranks[byte] != 0)
{
throw std::invalid_argument("string_radix_order: repeated alphabet byte");
}
#endif
ranks[byte] = next++;
}
for (auto &rank : ranks)
{
if (rank == 0)
{
rank = next++;
}
}
return ranks;
}
template <class Word>
std::vector<std::size_t> SortStringRadixIndices(std::span<const Word> words, std::string_view alphabet)
{
const auto rank = MakeStringRadixRanks(alphabet);
std::vector<std::size_t> order(words.size());
std::iota(order.begin(), order.end(), std::size_t{0});
if (words.size() < 2)
{
return order;
}
struct Group
{
std::size_t begin, end, depth;
};
std::vector<Group> active{{0, words.size(), 0}};
std::vector<std::size_t> dispersed(words.size());
std::vector<StringRadixDigit> digits(words.size());
while (!active.empty())
{
const auto [begin, end, depth] = active.back();
active.pop_back();
std::array<std::size_t, kStringRadixAlphabetSize + 1> counts{};
StringRadixDigit first = kStringRadixAlphabetSize, last = 0;
for (auto i = begin; i < end; ++i)
{
const auto &word = words[order[i]];
// 0は終端専用なので、入力の値0も普通のバイトとして扱える。
const StringRadixDigit digit = depth == word.size() ? 0 : rank[static_cast<unsigned char>(word[depth])];
digits[i] = digit;
++counts[digit];
first = std::min(first, digit);
last = std::max(last, digit);
}
if (first == last)
{
if (first != 0)
{
active.push_back({begin, end, depth + 1});
}
continue;
}
std::array<std::size_t, kStringRadixAlphabetSize + 1> offsets{};
auto next = begin;
for (auto digit = first; digit <= last; ++digit)
{
offsets[digit] = next;
next += counts[digit];
}
for (auto i = begin; i < end; ++i)
{
dispersed[offsets[digits[i]]++] = order[i];
}
std::copy(dispersed.begin() + begin, dispersed.begin() + end, order.begin() + begin);
for (auto digit = std::max(first, StringRadixDigit{1}); digit <= last; ++digit)
{
if (counts[digit] > 1)
{
const auto high = offsets[digit];
active.push_back({high - counts[digit], high, depth + 1});
}
}
}
return order;
}
} // namespace reinsirk::detail
[[nodiscard]] inline std::vector<std::size_t>
string_radix_order(std::span<const std::string_view> words, std::string_view alphabet = {})
{
return /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::SortStringRadixIndices(words, alphabet);
}
[[nodiscard]] inline std::vector<std::size_t>
string_radix_order(std::span<const std::string> words, std::string_view alphabet = {})
{
return /*SIRKIT_NS_REWRITE_V1:R:OjpyZWluc2lyazo6ZGV0YWls*/::sirkit::detail/*SIRKIT_NS_REWRITE_END_V1*/::SortStringRadixIndices(words, alphabet);
}
#endif
} // namespace sirkit
// SIRKIT_NAMESPACE_CLOSE_V1
// SIRKIT_NAMESPACE_END_V1