cp-library-cpp

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub naskya/cp-library-cpp

:heavy_check_mark: Edit distance
(include/string/edit_distance.hpp)

編集距離を求める関数が定義されています。


edit_distance(A, B)

AB編集距離を返します。A, B として文字列だけではなく std::vector<int> 等のコンテナを渡すこともできます。A, B の長さをそれぞれ $\lvert A \rvert, \lvert B \rvert$ とすると時間計算量は $\Theta(\lvert A \rvert \, \lvert B \rvert)$ です(ただし、要素の参照や整数の四則演算・コピー等の操作に掛かる時間が $\Theta(1)$ であることを仮定しています)。


Verified with

Code

//! @file edit_distance.hpp

#ifndef CP_LIBRARY_EDIT_DISTANCE_HPP
#define CP_LIBRARY_EDIT_DISTANCE_HPP

#include <vector>

namespace lib {

//! @tparam Container container type (deduced from parameters)
//! @param list_1 container 1
//! @param list_2 container 2
//! @return edit distance between container 1 and container 2
template <typename Container>
[[nodiscard]] int edit_distance(const Container& list_1, const Container& list_2) {
  const int n = static_cast<int>(std::size(list_1));
  const int m = static_cast<int>(std::size(list_2));

  std::vector dp(n + 1, std::vector<int>(m + 1));

  for (int i = 0; i <= n; ++i)
    dp[i][0] = i;
  for (int j = 0; j <= m; ++j)
    dp[0][j] = j;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = std::min(dp[i - 1][j], dp[i][j - 1]) + 1;
      dp[i][j] = std::min(dp[i][j], dp[i - 1][j - 1] + (list_1[i - 1] != list_2[j - 1]));
    }
  }

  return dp[n][m];
}

}  // namespace lib

#endif  // CP_LIBRARY_EDIT_DISTANCE_HPP
#line 1 "include/string/edit_distance.hpp"

//! @file edit_distance.hpp

#ifndef CP_LIBRARY_EDIT_DISTANCE_HPP
#define CP_LIBRARY_EDIT_DISTANCE_HPP

#include <vector>

namespace lib {

//! @tparam Container container type (deduced from parameters)
//! @param list_1 container 1
//! @param list_2 container 2
//! @return edit distance between container 1 and container 2
template <typename Container>
[[nodiscard]] int edit_distance(const Container& list_1, const Container& list_2) {
  const int n = static_cast<int>(std::size(list_1));
  const int m = static_cast<int>(std::size(list_2));

  std::vector dp(n + 1, std::vector<int>(m + 1));

  for (int i = 0; i <= n; ++i)
    dp[i][0] = i;
  for (int j = 0; j <= m; ++j)
    dp[0][j] = j;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = std::min(dp[i - 1][j], dp[i][j - 1]) + 1;
      dp[i][j] = std::min(dp[i][j], dp[i - 1][j - 1] + (list_1[i - 1] != list_2[j - 1]));
    }
  }

  return dp[n][m];
}

}  // namespace lib

#endif  // CP_LIBRARY_EDIT_DISTANCE_HPP
Back to top page