This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DPL_1_E"
#include <bits/stdc++.h>
#include "ugilib/base/constants.hpp"
#include "ugilib/base/definitions.hpp"
#include "ugilib/dp/edit_distance.hpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
// T
template<typename T> static T i() { T x; _cin >> x; return x; } // T item
// vector<T>
template<typename T> static vector<T> v(int n) {vector<T> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<T>
// vector<pair<T, T>>
template<typename T> static vector<pair<T, T>> vp(int n) {vector<pair<T, T>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<T, T>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for string
void p(const string& s) { cout << s << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
string s1 = rd::i<string>();
string s2 = rd::i<string>();
vector<char> v1(all(s1)), v2(all(s2));
auto dp = ugilib::edit_distance(v1, v2);
cout << dp.back().back() << endl;
// deb::p(dp);
return 0;
}
#line 1 "tests/dp/edit_distance.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/DPL_1_E"
#include <bits/stdc++.h>
#line 2 "ugilib/base/definitions.hpp"
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define rep(i, n) for(size_t i = 0; i < n; i++) // rep macro
#define all(v) begin(v), end(v) // all iterator
#line 3 "ugilib/base/constants.hpp"
namespace ugilib::constants {
template<typename T>
inline constexpr T INF = std::numeric_limits<T>::max() / 2;
} // namespace ugilib::constants
const ll INF = ugilib::constants::INF<ll>;
#line 4 "ugilib/dp/edit_distance.hpp"
using namespace std;
namespace ugilib {
/**
* @brief 編集距離
* @tparam T vectorの要素の型. stringだったらvector<char>で渡す
* @param v1 vector1
* @param v2 vector2
* @return vector<vector<ll>> dp[i][j]: v1のi文字目までとv2のj文字目までの編集距離
* @note O(nm). n = v1.size(), m = v2.size()
* @details
* auto dp = edit_distance(v1, v2);
* cout << dp[v1.size()][v2.size()] << endl;
* cout << dp.back().back() << endl;
*/
template <typename T>
vector<vector<ll>> edit_distance(const vector<T>& v1, const vector<T>& v2) {
int n = v1.size();
int m = v2.size();
// dp[i][j]: v1のi文字目までとv2のj文字目までの編集距離
vector<vector<ll>> dp(n+1, vector<ll>(m+1, INF));
// 空文字列との編集距離
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 = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dp[i+1][j+1] = min(dp[i][j+1] + 1, dp[i+1][j] + 1); // v1 or v2どちらか1文字追加
if (v1[i] == v2[j]) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]); // v1 and v2両方から1文字追加
else dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + 1); // v1 and v2両方から1文字追加し, 片方を変更する
}
}
return dp;
}
} // namespace ugilib
#line 7 "tests/dp/edit_distance.test.cpp"
using namespace std;
// debug settings
// #define DEBUG
#ifdef DEBUG
// debug input
string _INPUT = R"(
5
1 2 3 4 5
)";
auto _cin = stringstream(_INPUT.substr(1)); // remove '\n' at _INPUT[0]
#else
// standard input
auto& _cin = cin;
#endif
// speed up
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// reader
struct rd {
// T
template<typename T> static T i() { T x; _cin >> x; return x; } // T item
// vector<T>
template<typename T> static vector<T> v(int n) {vector<T> v(n); rep(i, n) _cin >> v[i]; return v;} // vector<T>
// vector<pair<T, T>>
template<typename T> static vector<pair<T, T>> vp(int n) {vector<pair<T, T>> v(n); rep(i, n) _cin >> v[i].first >> v[i].second; return v;} // vector<pair<T, T>>
// tuple
template<typename... Args> static tuple<Args...> t() {
tuple<Args...> values;
apply([](auto&... args) { ((_cin >> args), ...); }, values);
return values;
}
};
// debug print utility
namespace deb {
#include <cxxabi.h>
// demangle type name
string demangle(const char* name) {
int status = -4;
unique_ptr<char, void(*)(void*)> res{
abi::__cxa_demangle(name, NULL, NULL, &status),
free
};
return (status == 0) ? string(res.get()) : name ;
}
// meta functions for type traits
template<typename T>
constexpr bool isArithmeticContainer() { return is_arithmetic<typename T::value_type>::value; }
// for SFINAE
template<typename T, typename = void> struct has_key_and_mapped_type : false_type {};
template<typename T> struct has_key_and_mapped_type<T, void_t<typename T::key_type, typename T::mapped_type>> : true_type {};
// for map or unordered_map
template<typename T>
constexpr bool isMapLike() { return has_key_and_mapped_type<T>::value; }
// for values
template<typename T, typename enable_if<is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& x) { cout << x << " "; }
// for pairs
template <typename T, typename S>
void p(const pair<T, S>& _p){ p(_p.first); p(_p.second); cout << endl; }
// for string
void p(const string& s) { cout << s << endl; }
// for containers
template<typename T, typename enable_if<!is_arithmetic<T>::value, nullptr_t>::type = nullptr>
void p(const T& container) {
// map and unordered_map
if constexpr (isMapLike<T>()) {
cout << demangle(typeid(T).name()) << ":" << endl;
for (const auto& kv : container) {
cout << "[" << kv.first << "] => ";
p(kv.second);
if constexpr (is_arithmetic_v<typename T::mapped_type>) cout << endl;
}
// vector or set or others
} else {
if constexpr (!isArithmeticContainer<T>()) cout << demangle(typeid(T).name()) << ":" << endl;
for (auto it = begin(container); it != end(container); ++it) {
p(*it);
}
if constexpr (isArithmeticContainer<T>()) cout << endl;
}
}
}; // namespace deb
int main() {
auto& cin = _cin;
// speed up io
cin.tie(nullptr);
ios::sync_with_stdio(false);
// code
string s1 = rd::i<string>();
string s2 = rd::i<string>();
vector<char> v1(all(s1)), v2(all(s2));
auto dp = ugilib::edit_distance(v1, v2);
cout << dp.back().back() << endl;
// deb::p(dp);
return 0;
}