:heavy_check_mark: test/aoj_DPL_1_E.test.cpp

Back to top page

Depends on

Code

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

#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>

#include "utility/make/vector.cpp"

int main() {
  char buf[1024];
  scanf("%s", buf);
  std::string s = buf;
  scanf("%s", buf);
  std::string t = buf;

  size_t n = s.length();
  size_t m = t.length();
  auto dp = make_vector<int>({n+1, m+1}, 0);
  for (size_t i = 1; i <= n; ++i) dp[i][0] = i;
  for (size_t j = 1; j <= m; ++j) dp[0][j] = j;

  for (size_t i = 1; i <= n; ++i)
    for (size_t 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] + ((s[i-1] == t[j-1])? 0: 1)});
    }

  printf("%d\n", dp[n][m]);
}

#line 1 "test/aoj_DPL_1_E.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_E"

#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>

#line 1 "utility/make/vector.cpp"



/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */

#include <cstddef>
#include <type_traits>
#line 12 "utility/make/vector.cpp"

namespace detail {
  template <typename Tp, size_t Nb>
  auto make_vector(std::vector<size_t>& sizes, Tp const& x) {
    if constexpr (Nb == 1) {
      return std::vector(sizes[0], x);
    } else {
      size_t size = sizes[Nb-1];
      sizes.pop_back();
      return std::vector(size, make_vector<Tp, Nb-1>(sizes, x));
    }
  }
}  // detail::

template <typename Tp, size_t Nb>
auto make_vector(size_t const(&sizes)[Nb], Tp const& x = Tp()) {
  std::vector<size_t> s(Nb);
  for (size_t i = 0; i < Nb; ++i) s[i] = sizes[Nb-i-1];
  return detail::make_vector<Tp, Nb>(s, x);
}


#line 10 "test/aoj_DPL_1_E.test.cpp"

int main() {
  char buf[1024];
  scanf("%s", buf);
  std::string s = buf;
  scanf("%s", buf);
  std::string t = buf;

  size_t n = s.length();
  size_t m = t.length();
  auto dp = make_vector<int>({n+1, m+1}, 0);
  for (size_t i = 1; i <= n; ++i) dp[i][0] = i;
  for (size_t j = 1; j <= m; ++j) dp[0][j] = j;

  for (size_t i = 1; i <= n; ++i)
    for (size_t 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] + ((s[i-1] == t[j-1])? 0: 1)});
    }

  printf("%d\n", dp[n][m]);
}

Back to top page