shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/string/kmp.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// prefix_function、kmp_search を愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#include "../../../src/string/kmp.hpp"

#include <cassert>
#include <iostream>
#include <random>
#include <string>
#include <vector>
#if __cplusplus >= 202002L
#include <ranges>
#endif

template <class R>
std::vector<long long> collect(const R& matches) {
    return {matches.begin(), matches.end()};
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 5000 && ok; rep++) {
        int n = rng() % 30;
        int k = rng() % 3 + 1;
        std::string s(n, 'a');
        for (auto& c : s) c = 'a' + rng() % k;

        std::vector<long long> pi(n);
        for (int i = 0; i < n; i++) {
            for (int l = 1; l <= i; l++) {
                if (s.substr(0, l) == s.substr(i + 1 - l, l)) pi[i] = l;
            }
        }
        ok = ok && prefix_function(s) == pi && prefix_function(s.begin(), s.end()) == pi;

        int m = rng() % 4;
        std::string p(m, 'a');
        for (auto& c : p) c = 'a' + rng() % k;
        std::vector<long long> occ;
        for (int i = 0; i + m <= n; i++) {
            if (s.substr(i, m) == p) occ.push_back(i);
        }
        ok = ok && collect(kmp_search(s, p)) == occ;
    }

    // 例
    ok = ok && prefix_function(std::string("aabaaab")) == std::vector<long long>{0, 1, 0, 1, 2, 2, 3};
    ok = ok && collect(kmp_search(std::string("aaaa"), std::string("aa"))) == std::vector<long long>{0, 1, 2};
    ok = ok && collect(kmp_search(std::vector<int>{1, 2, 1, 2, 1}, std::vector<int>{1, 2, 1})) == std::vector<long long>{0, 2};

    // 失敗リンクを何段も戻る場合と、重なりのある一致を含む二値列の全探索。
    for (long long n = 0; n <= 8; n++) {
        for (long long mask = 0; mask < (1LL << n); mask++) {
            std::string text(n, 'a');
            for (long long i = 0; i < n; i++) text[i] += (mask >> i) & 1;
            for (long long m = 0; m <= 5; m++) {
                for (long long bits = 0; bits < (1LL << m); bits++) {
                    std::string pattern(m, 'a');
                    for (long long i = 0; i < m; i++) pattern[i] += (bits >> i) & 1;
                    std::vector<long long> expected;
                    for (long long i = 0; i + m <= n; i++) {
                        if (text.compare(i, m, pattern) == 0) expected.push_back(i);
                    }
                    ok = ok && collect(kmp_search(text, pattern)) == expected;
                }
            }
        }
    }

    // 空のパターン・空の入力・一致なし・パターンの方が長い場合。
    ok = ok && collect(kmp_search(std::string(), std::string())) == std::vector<long long>{0};
    ok = ok && collect(kmp_search(std::string("ab"), std::string())) == std::vector<long long>{0, 1, 2};
    ok = ok && collect(kmp_search(std::string(), std::string("a"))).empty();
    ok = ok && collect(kmp_search(std::string("a"), std::string("aa"))).empty();
    ok = ok && collect(kmp_search(std::string("aaaa"), std::string("b"))).empty();

    // range が一時入力を所有し、コピー・移動後も自分の入力を走査する。
    {
        auto make_matches = [] { return kmp_search(std::string("aaaa"), std::string("aa")); };
        const auto matches = make_matches();
        auto copied = matches;
        auto moved = std::move(copied);
        const std::vector<long long> expected = {0, 1, 2};
        for (int rep = 0; rep < 2; rep++) {
            ok = ok && collect(matches) == expected && collect(moved) == expected;
        }
        auto it = matches.begin(), independent = it;
        ok = ok && *it++ == 0 && *it == 1 && *independent == 0;
        long long pairs = 0;
        for (long long x : matches) {
            for (long long y : matches) pairs += x == y;
        }
        ok = ok && pairs == 3;
#if __cplusplus >= 202002L
        static_assert(std::ranges::input_range<decltype(matches)>);
#endif
    }
    // 左辺値をコピーしない。一時入力は move-only な型でも所有できる。
    {
        struct Series {
            std::vector<char> values;
            explicit Series(const std::string& s) : values(s.begin(), s.end()) {}
            Series(const Series&) = delete;
            Series(Series&&) = default;
            auto begin() const { return values.begin(); }
            auto end() const { return values.end(); }
        };
        const Series text("ababab"), pattern("aba");
        ok = ok && collect(kmp_search(text, pattern)) == std::vector<long long>{0, 2};
        auto matches = kmp_search(Series("ababab"), Series("aba"));
        auto moved = std::move(matches);
        ok = ok && collect(moved) == std::vector<long long>{0, 2};
    }
    // 構築時は text を読まず、最初の一致で終了すると後ろを比較しない。
    {
        struct Character {
            long long* reads;
            bool operator==(char c) const {
                ++*reads;
                return c == 'a';
            }
        };
        long long reads = 0;
        const std::vector<Character> text(100, Character{&reads});
        const auto matches = kmp_search(text, std::string("a"));
        ok = ok && reads == 0;
        for (long long pos : matches) {
            ok = ok && pos == 0;
            break;
        }
        ok = ok && reads == 1;
    }
    assert(ok);

    if (ok)
        std::cout << "Hello World" << std::endl;
    else
        std::cerr << "failed" << std::endl;
}
#line 1 "test/unittest/string/kmp.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"

// prefix_function、kmp_search を愚直な方法とランダムに比較する
// 問題の入力は使わず、すべて一致したら Hello World を出力する

#line 2 "src/string/kmp.hpp"

/**
 * @file kmp.hpp
 * @brief 接頭辞関数と KMP 法
 */

#include <cstddef>
#include <iterator>
#include <utility>
#include <vector>

/**
 * @brief 接頭辞関数 (KMP 法の失敗関数)
 * s[0, i] の接頭辞であり接尾辞でもある部分 (s[0, i] 自身を除く) の最長の長さを各 i について求める 全体で O(N)
 *
 * @tparam It 配列のイテレータ
 * @param begin 配列の開始イテレータ
 * @param end 配列の終了イテレータ
 * @return std::vector<long long> pi
 */
template <typename It>
std::vector<long long> prefix_function(It begin, It end) {
    long long n = end - begin;
    std::vector<long long> pi(n);
    for (long long i = 1; i < n; i++) {
        long long j = pi[i - 1];
        while (j > 0 && !(begin[i] == begin[j])) j = pi[j - 1];
        if (begin[i] == begin[j]) j++;
        pi[i] = j;
    }
    return pi;
}

/**
 * @brief 接頭辞関数 (KMP 法の失敗関数)
 * s[0, i] の接頭辞であり接尾辞でもある部分 (s[0, i] 自身を除く) の最長の長さを各 i について求める 全体で O(N)
 *
 * @tparam R 配列の型
 * @param s 配列
 * @return std::vector<long long> pi
 */
template <typename R>
std::vector<long long> prefix_function(const R& s) {
    return prefix_function(std::begin(s), std::end(s));
}

namespace internal_kmp {
template <typename Text, typename Pattern>
class Matches {
    Text text;
    Pattern pattern;
    std::vector<long long> pi;
    long long n, m;

   public:
    Matches(Text&& text, Pattern&& pattern)
        : text(std::forward<Text>(text)), pattern(std::forward<Pattern>(pattern)), pi(prefix_function(this->pattern)), n(std::end(std::as_const(this->text)) - std::begin(std::as_const(this->text))), m(pi.size()) {}

    class iterator {
        const Matches* range = nullptr;
        long long i = 0, j = 0, pos = -1;

       public:
        using value_type = long long;
        using difference_type = std::ptrdiff_t;
        using reference = value_type;
        using pointer = void;
        using iterator_category = std::input_iterator_tag;

        iterator() = default;
        explicit iterator(const Matches* range) : range(range) { ++*this; }
        value_type operator*() const { return pos; }
        iterator& operator++() {
            if (range->m == 0) {
                pos = i;
                i = i == range->n ? -1 : i + 1;
                return *this;
            }
            auto t = std::begin(std::as_const(range->text));
            auto p = std::begin(std::as_const(range->pattern));
            while (i < range->n) {
                while (j > 0 && !(t[i] == p[j])) j = range->pi[j - 1];
                if (t[i] == p[j]) j++;
                i++;
                if (j == range->m) {
                    pos = i - range->m;
                    j = range->pi[j - 1];
                    return *this;
                }
            }
            pos = -1;
            return *this;
        }
        iterator operator++(int) {
            auto previous = *this;
            ++*this;
            return previous;
        }
        bool operator==(const iterator& other) const {
            if (pos == -1 || other.pos == -1) return pos == other.pos;
            return range == other.range && pos == other.pos;
        }
        bool operator!=(const iterator& other) const { return !(*this == other); }
    };

    iterator begin() const { return iterator(this); }
    iterator end() const { return iterator(); }
};
}  // namespace internal_kmp

/**
 * @brief KMP 法による一致位置の遅延列挙。構築 O(|pattern|)、全走査 O(|text| + |pattern|)、追加領域 O(|pattern|)
 * pattern が空なら 0, 1, ..., |text| を返す。左辺値の入力は参照し、右辺値は所有する
 * @pre 両方の入力にランダムアクセス可能な iterator があること
 * @pre 走査中は range と参照する入力を有効なまま保持し、変更しないこと
 */
template <typename R1, typename R2>
auto kmp_search(R1&& text, R2&& pattern) {
    return internal_kmp::Matches<R1, R2>(std::forward<R1>(text), std::forward<R2>(pattern));
}
#line 7 "test/unittest/string/kmp.test.cpp"

#include <cassert>
#include <iostream>
#include <random>
#include <string>
#line 13 "test/unittest/string/kmp.test.cpp"
#if __cplusplus >= 202002L
#include <ranges>
#endif

template <class R>
std::vector<long long> collect(const R& matches) {
    return {matches.begin(), matches.end()};
}

int main() {
    std::mt19937 rng(314);
    bool ok = true;
    for (int rep = 0; rep < 5000 && ok; rep++) {
        int n = rng() % 30;
        int k = rng() % 3 + 1;
        std::string s(n, 'a');
        for (auto& c : s) c = 'a' + rng() % k;

        std::vector<long long> pi(n);
        for (int i = 0; i < n; i++) {
            for (int l = 1; l <= i; l++) {
                if (s.substr(0, l) == s.substr(i + 1 - l, l)) pi[i] = l;
            }
        }
        ok = ok && prefix_function(s) == pi && prefix_function(s.begin(), s.end()) == pi;

        int m = rng() % 4;
        std::string p(m, 'a');
        for (auto& c : p) c = 'a' + rng() % k;
        std::vector<long long> occ;
        for (int i = 0; i + m <= n; i++) {
            if (s.substr(i, m) == p) occ.push_back(i);
        }
        ok = ok && collect(kmp_search(s, p)) == occ;
    }

    // 例
    ok = ok && prefix_function(std::string("aabaaab")) == std::vector<long long>{0, 1, 0, 1, 2, 2, 3};
    ok = ok && collect(kmp_search(std::string("aaaa"), std::string("aa"))) == std::vector<long long>{0, 1, 2};
    ok = ok && collect(kmp_search(std::vector<int>{1, 2, 1, 2, 1}, std::vector<int>{1, 2, 1})) == std::vector<long long>{0, 2};

    // 失敗リンクを何段も戻る場合と、重なりのある一致を含む二値列の全探索。
    for (long long n = 0; n <= 8; n++) {
        for (long long mask = 0; mask < (1LL << n); mask++) {
            std::string text(n, 'a');
            for (long long i = 0; i < n; i++) text[i] += (mask >> i) & 1;
            for (long long m = 0; m <= 5; m++) {
                for (long long bits = 0; bits < (1LL << m); bits++) {
                    std::string pattern(m, 'a');
                    for (long long i = 0; i < m; i++) pattern[i] += (bits >> i) & 1;
                    std::vector<long long> expected;
                    for (long long i = 0; i + m <= n; i++) {
                        if (text.compare(i, m, pattern) == 0) expected.push_back(i);
                    }
                    ok = ok && collect(kmp_search(text, pattern)) == expected;
                }
            }
        }
    }

    // 空のパターン・空の入力・一致なし・パターンの方が長い場合。
    ok = ok && collect(kmp_search(std::string(), std::string())) == std::vector<long long>{0};
    ok = ok && collect(kmp_search(std::string("ab"), std::string())) == std::vector<long long>{0, 1, 2};
    ok = ok && collect(kmp_search(std::string(), std::string("a"))).empty();
    ok = ok && collect(kmp_search(std::string("a"), std::string("aa"))).empty();
    ok = ok && collect(kmp_search(std::string("aaaa"), std::string("b"))).empty();

    // range が一時入力を所有し、コピー・移動後も自分の入力を走査する。
    {
        auto make_matches = [] { return kmp_search(std::string("aaaa"), std::string("aa")); };
        const auto matches = make_matches();
        auto copied = matches;
        auto moved = std::move(copied);
        const std::vector<long long> expected = {0, 1, 2};
        for (int rep = 0; rep < 2; rep++) {
            ok = ok && collect(matches) == expected && collect(moved) == expected;
        }
        auto it = matches.begin(), independent = it;
        ok = ok && *it++ == 0 && *it == 1 && *independent == 0;
        long long pairs = 0;
        for (long long x : matches) {
            for (long long y : matches) pairs += x == y;
        }
        ok = ok && pairs == 3;
#if __cplusplus >= 202002L
        static_assert(std::ranges::input_range<decltype(matches)>);
#endif
    }
    // 左辺値をコピーしない。一時入力は move-only な型でも所有できる。
    {
        struct Series {
            std::vector<char> values;
            explicit Series(const std::string& s) : values(s.begin(), s.end()) {}
            Series(const Series&) = delete;
            Series(Series&&) = default;
            auto begin() const { return values.begin(); }
            auto end() const { return values.end(); }
        };
        const Series text("ababab"), pattern("aba");
        ok = ok && collect(kmp_search(text, pattern)) == std::vector<long long>{0, 2};
        auto matches = kmp_search(Series("ababab"), Series("aba"));
        auto moved = std::move(matches);
        ok = ok && collect(moved) == std::vector<long long>{0, 2};
    }
    // 構築時は text を読まず、最初の一致で終了すると後ろを比較しない。
    {
        struct Character {
            long long* reads;
            bool operator==(char c) const {
                ++*reads;
                return c == 'a';
            }
        };
        long long reads = 0;
        const std::vector<Character> text(100, Character{&reads});
        const auto matches = kmp_search(text, std::string("a"));
        ok = ok && reads == 0;
        for (long long pos : matches) {
            ok = ok && pos == 0;
            break;
        }
        ok = ok && reads == 1;
    }
    assert(ok);

    if (ok)
        std::cout << "Hello World" << std::endl;
    else
        std::cerr << "failed" << std::endl;
}
Back to top page