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: test/string/edit_distance/1.test.cpp

Depends on

Code

#define PROBLEM "https://atcoder.jp/contests/abc185/tasks/abc185_e"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include "../../../include/string/edit_distance.hpp"

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;

  std::vector<int> A(N), B(M);
  std::copy_n(std::istream_iterator<int>(std::cin), N, std::begin(A));
  std::copy_n(std::istream_iterator<int>(std::cin), M, std::begin(B));

  std::cout << lib::edit_distance(A, B) << '\n';
}
#line 1 "test/string/edit_distance/1.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc185/tasks/abc185_e"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

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

//! @file edit_distance.hpp

#ifndef CP_LIBRARY_EDIT_DISTANCE_HPP
#define CP_LIBRARY_EDIT_DISTANCE_HPP

#line 8 "include/string/edit_distance.hpp"

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 8 "test/string/edit_distance/1.test.cpp"

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N, M;
  std::cin >> N >> M;

  std::vector<int> A(N), B(M);
  std::copy_n(std::istream_iterator<int>(std::cin), N, std::begin(A));
  std::copy_n(std::istream_iterator<int>(std::cin), M, std::begin(B));

  std::cout << lib::edit_distance(A, B) << '\n';
}
Back to top page