shogo-library

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

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: 編集距離 (レーベンシュタイン距離)
(src/dp/edit-distance.hpp)

列 s を列 t に変えるのに必要な、1 要素の挿入・削除・置換の最小回数を $O(nm)$ で求めます。$n$、$m$ は s、t の長さです。文字列のほか、vector などの列にも使えます (要素は == で比べます)。

long long d = edit_distance(std::string("kitten"), std::string("sitting"));  // 3
edit_distance(std::vector<int>{1, 2, 3}, std::vector<int>{2, 3, 4});        // 2

edit_distance_restore は距離と、最小回数の挿入・削除・置換を行う操作列を返します。入力はランダムアクセス可能な列にしてください。最適な操作列が複数ある場合は、そのうち 1 つを返します。

std::string s = "kitten", t = "sitting";
auto [distance, operations] = edit_distance_restore(s, t);  // distance == 3
for (const auto& op : operations) {
    if (op.type == EditDistance::Type::Insert) s.insert(s.begin() + op.target_index, t[op.target_index]);
    if (op.type == EditDistance::Type::Erase) s.erase(s.begin() + op.target_index);
    if (op.type == EditDistance::Type::Replace) s[op.target_index] = t[op.target_index];
}
// s == t

source_index は変更前の元の列の添字で、挿入のときは隙間の位置 [0, |s|] です。target_index は目標の列の添字で、削除のときは隙間の位置 [0, |t|] です。返された順に操作を適用する場合、変更中の列で操作する位置も target_index になります。挿入・置換する値は元の目標列の t[target_index] から取得してください。値を操作列にコピーしないため、文字列以外の列でも同じ型で結果を受け取れます。

一致した要素を通過する操作は含みません。操作数は距離と等しく、同じ列どうしでは空の操作列になります。

関数

関数 説明 計算量
edit_distance(s, t) 編集距離 (long long) 時間 $O(nm)$、メモリ $O(m)$
edit_distance_restore(s, t) 距離と操作列を持つ EditDistance を返す 時間・メモリ $O(nm)$

メンバ変数

メンバ 説明
EditDistance::distance 編集距離 (long long)
EditDistance::operations 順に適用する Operation の列
Operation::type EditDistance::Type::Insert・Erase・Replace
Operation::source_index 元の列の添字 (挿入では隙間)
Operation::target_index 目標の列の添字 (削除では隙間)

関連

Verified with

Code

#pragma once

/**
 * @file edit-distance.hpp
 * @brief 編集距離 (レーベンシュタイン距離)
 */

#include <algorithm>
#include <cassert>
#include <iterator>
#include <vector>

/**
 * @brief s を t に変えるのに必要な挿入・削除・置換の最小回数 O(nm)
 * 要素は == で比べる。メモリは O(m)
 */
template <typename R1, typename R2>
long long edit_distance(const R1& s, const R2& t) {
    auto a = std::begin(s);
    auto b = std::begin(t);
    long long n = std::end(s) - a, m = std::end(t) - b;
    // dp[j] は s[0, i) と t[0, j) の編集距離
    std::vector<long long> dp(m + 1);
    for (long long j = 0; j <= m; j++) dp[j] = j;
    for (long long i = 1; i <= n; i++) {
        long long diag = dp[0];  // s[0, i - 1) と t[0, j - 1) の編集距離
        dp[0] = i;
        for (long long j = 1; j <= m; j++) {
            long long up = dp[j];
            dp[j] = std::min({dp[j] + 1, dp[j - 1] + 1, diag + (a[i - 1] == b[j - 1] ? 0 : 1)});
            diag = up;
        }
    }
    return dp[m];
}

/**
 * @brief 編集距離と、s を t に変える操作列
 */
struct EditDistance {
    enum class Type { Insert,
                      Erase,
                      Replace };
    struct Operation {
        Type type;
        long long source_index;  //!< 元の s の添字。挿入では隙間の位置 [0, |s|]
        long long target_index;  //!< t の添字。削除では隙間の位置 [0, |t|]
    };
    long long distance;
    std::vector<Operation> operations;
};

/**
 * @brief 編集距離と、その最小回数の挿入・削除・置換を求める
 * 時間・メモリ O(nm)。一致する要素を通過する操作は含めない。
 * 操作順に、変更中の列の target_index の位置へ挿入・削除・置換する。
 * 挿入・置換する値は t[target_index]。
 */
template <typename R1, typename R2>
EditDistance edit_distance_restore(const R1& s, const R2& t) {
    auto a = std::begin(s);
    auto b = std::begin(t);
    const long long n = std::end(s) - a, m = std::end(t) - b;
    std::vector<std::vector<long long>> dp(n + 1, std::vector<long long>(m + 1));
    for (long long i = 0; i <= n; ++i) dp[i][0] = i;
    for (long long j = 0; j <= m; ++j) dp[0][j] = j;
    for (long long i = 1; i <= n; ++i) {
        for (long long j = 1; j <= m; ++j) {
            dp[i][j] = std::min({dp[i - 1][j] + 1, dp[i][j - 1] + 1,
                                 dp[i - 1][j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1)});
        }
    }
    EditDistance result{dp[n][m], {}};
    result.operations.reserve(result.distance);
    long long i = n, j = m;
    while (i > 0 || j > 0) {
        if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1)) {
            if (!(a[i - 1] == b[j - 1])) result.operations.push_back({EditDistance::Type::Replace, i - 1, j - 1});
            --i, --j;
        } else if (i > 0 && dp[i][j] == dp[i - 1][j] + 1) {
            result.operations.push_back({EditDistance::Type::Erase, i - 1, j});
            --i;
        } else {
            assert(j > 0);
            result.operations.push_back({EditDistance::Type::Insert, i, j - 1});
            --j;
        }
    }
    std::reverse(result.operations.begin(), result.operations.end());
    return result;
}
#line 2 "src/dp/edit-distance.hpp"

/**
 * @file edit-distance.hpp
 * @brief 編集距離 (レーベンシュタイン距離)
 */

#include <algorithm>
#include <cassert>
#include <iterator>
#include <vector>

/**
 * @brief s を t に変えるのに必要な挿入・削除・置換の最小回数 O(nm)
 * 要素は == で比べる。メモリは O(m)
 */
template <typename R1, typename R2>
long long edit_distance(const R1& s, const R2& t) {
    auto a = std::begin(s);
    auto b = std::begin(t);
    long long n = std::end(s) - a, m = std::end(t) - b;
    // dp[j] は s[0, i) と t[0, j) の編集距離
    std::vector<long long> dp(m + 1);
    for (long long j = 0; j <= m; j++) dp[j] = j;
    for (long long i = 1; i <= n; i++) {
        long long diag = dp[0];  // s[0, i - 1) と t[0, j - 1) の編集距離
        dp[0] = i;
        for (long long j = 1; j <= m; j++) {
            long long up = dp[j];
            dp[j] = std::min({dp[j] + 1, dp[j - 1] + 1, diag + (a[i - 1] == b[j - 1] ? 0 : 1)});
            diag = up;
        }
    }
    return dp[m];
}

/**
 * @brief 編集距離と、s を t に変える操作列
 */
struct EditDistance {
    enum class Type { Insert,
                      Erase,
                      Replace };
    struct Operation {
        Type type;
        long long source_index;  //!< 元の s の添字。挿入では隙間の位置 [0, |s|]
        long long target_index;  //!< t の添字。削除では隙間の位置 [0, |t|]
    };
    long long distance;
    std::vector<Operation> operations;
};

/**
 * @brief 編集距離と、その最小回数の挿入・削除・置換を求める
 * 時間・メモリ O(nm)。一致する要素を通過する操作は含めない。
 * 操作順に、変更中の列の target_index の位置へ挿入・削除・置換する。
 * 挿入・置換する値は t[target_index]。
 */
template <typename R1, typename R2>
EditDistance edit_distance_restore(const R1& s, const R2& t) {
    auto a = std::begin(s);
    auto b = std::begin(t);
    const long long n = std::end(s) - a, m = std::end(t) - b;
    std::vector<std::vector<long long>> dp(n + 1, std::vector<long long>(m + 1));
    for (long long i = 0; i <= n; ++i) dp[i][0] = i;
    for (long long j = 0; j <= m; ++j) dp[0][j] = j;
    for (long long i = 1; i <= n; ++i) {
        for (long long j = 1; j <= m; ++j) {
            dp[i][j] = std::min({dp[i - 1][j] + 1, dp[i][j - 1] + 1,
                                 dp[i - 1][j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1)});
        }
    }
    EditDistance result{dp[n][m], {}};
    result.operations.reserve(result.distance);
    long long i = n, j = m;
    while (i > 0 || j > 0) {
        if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1)) {
            if (!(a[i - 1] == b[j - 1])) result.operations.push_back({EditDistance::Type::Replace, i - 1, j - 1});
            --i, --j;
        } else if (i > 0 && dp[i][j] == dp[i - 1][j] + 1) {
            result.operations.push_back({EditDistance::Type::Erase, i - 1, j});
            --i;
        } else {
            assert(j > 0);
            result.operations.push_back({EditDistance::Type::Insert, i, j - 1});
            --j;
        }
    }
    std::reverse(result.operations.begin(), result.operations.end());
    return result;
}
Back to top page