shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/unittest/string/z-algorithm.test.cpp

Depends on

Code

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

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

#include "../../../src/string/z-algorithm.hpp"

#include <iostream>
#include <random>
#include <string>
#include <vector>

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> z(n);
        for (int i = 0; i < n; i++) {
            while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
        }
        ok = ok && z_algorithm(s) == z && z_algorithm(s.begin(), s.end()) == z;
    }
    ok = ok && z_algorithm(std::string("aabaab")) == std::vector<long long>{6, 1, 0, 3, 1, 0};
    ok = ok && z_algorithm(std::vector<int>{}).empty();

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

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

#line 2 "src/string/z-algorithm.hpp"

/**
 * @file z-algorithm.hpp
 * @brief Z-algorithm
 */

#include <iterator>
#include <vector>

/**
 * @brief Z-algorithm
 * s[0:]とs[i:]のLCPの長さを各iについて求める 全体でO(N)
 *
 * @tparam It 配列のイテレータ
 * @param begin 配列の開始イテレータ
 * @param end 配列の終了イテレータ
 * @return std::vector<long long> z
 */
template <typename It>
std::vector<long long> z_algorithm(It begin, It end) {
    long long n = end - begin;
    if (n == 0) return {};
    std::vector<long long> z(n);
    z[0] = n;
    for (long long i = 1, j = 0; i < n;) {
        // [0, j)と[i, i + j)が一致している
        while (i + j < n && begin[j] == begin[i + j]) j++;
        z[i] = j;
        long long k = 1;  // [k,)と[i+k,)はLCPが等しいと確定している
        for (; k < j && k + z[k] < j; k++) {
            z[i + k] = z[k];
        }
        i += k;
        if (j) j -= k;
    }
    return z;
}

/**
 * @brief Z-algorithm
 * s[0:]とs[i:]のLCPの長さを各iについて求める 全体でO(N)
 *
 * @tparam R 配列の型
 * @param s 配列
 * @return std::vector<long long> z
 */
template <typename R>
std::vector<long long> z_algorithm(const R& s) {
    return z_algorithm(std::begin(s), std::end(s));
}
#line 7 "test/unittest/string/z-algorithm.test.cpp"

#include <iostream>
#include <random>
#include <string>
#line 12 "test/unittest/string/z-algorithm.test.cpp"

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> z(n);
        for (int i = 0; i < n; i++) {
            while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++;
        }
        ok = ok && z_algorithm(s) == z && z_algorithm(s.begin(), s.end()) == z;
    }
    ok = ok && z_algorithm(std::string("aabaab")) == std::vector<long long>{6, 1, 0, 3, 1, 0};
    ok = ok && z_algorithm(std::vector<int>{}).empty();

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