// https://yukicoder.me/problems/610 #include using namespace std; #define times(n, i) uptil(0, n, i) #define rtimes(n, i) downto((n) - 1, 0, i) #define upto(f, t, i) for(decltype(t) i##0_to = (t), i = (f); i <= i##0_to; i++) #define uptil(f, t, i) for(decltype(t) i##0_to = (t), i = (f); i < i##0_to; i++) #define downto(f, t, i) for(decltype(f) i##0_to = (t), i = (f); i >= i##0_to; i--) #define downtil(f, t, i) for(decltype(f) i##0_to = (t), i = (f); i > i##0_to; i--) // types typedef long double LD; #define long long long #define int long using VI = vector; using WI = vector; using PI = pair; using VPI = vector; using WPI = vector; bool debug; #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 2 #define _LIBCPP_DEBUG2 2 #define ln << '\n' #define tb << '\t' #define sp << ' ' #define db(x) if(debug) cerr << #x << " = " << (x) << ", " #define dbg(x) if(debug) cerr << #x << " = " << (x) ln constexpr long INF = 1LL << 60; void solve(); signed main(signed argc, char *argv[]) { #ifdef EBUG debug = true; #elif defined(ONLINE_JUDGE) debug = false; #else debug = argc >= 2; #endif if(!debug) { cin.tie(0); ios::sync_with_stdio(0); } cout << fixed << setprecision(20); cerr << fixed << setprecision(20); solve(); return 0; } /******************************* basic library ********************************/ // IO template istream& operator>>(istream& s, vector& v) { for(auto&& p : v) s >> p; return s; } template ostream& operator<<(ostream& s, vector v) { int i = 0; for(const auto& p : v) s << (i++?" ":"") << p; return s; } template ostream& operator<<(ostream& s, vector> w) { for(const auto& v : w) { int i = 0; for(const auto& p : v) s << (i++?" ":"") << p; s ln; } return s; } template T RD(A... a) { T t(a...); cin >> t; return t; } // other #define all(x) (x).begin(), (x).end() #define b_max(x, y) x = max(x, y) #define b_min(x, y) x = min(x, y) inline LD AC(LD d) { return d ? d : 0; } /****************************** optional library ******************************/ //-mod constexpr long MOD = 1000000009; // 1000000007; // 998244353; //-math //-rmq //-dfs //-sparse_table(fn,nil=min,INF) //+string/edit_distance template int edit_distance( vector a, vector b, int ins = 1, int sub = 1, int pass = 0 ) { if(ins < 0) { db(ins); dbg(ins >= 0); } if(sub < 0) { db(sub); dbg(sub >= 0); } if(pass > 0) { db(pass); dbg(sub <= 0); } int x = a.size(), y = b.size(); WI dp(x + 1, VI(y + 1)); upto(1, x, i) dp[i][0] = dp[i-1][0] + ins; upto(1, y, j) dp[0][j] = dp[0][j-1] + ins; upto(1, x, i) upto(1, y, j) { dp[i][j] = min({ dp[i-1][j] + ins, dp[i][j-1] + ins, dp[i-1][j-1] + (a[i-1] == b[j-1] ? pass : sub), }); } return dp[x][y]; } /************************************ main ************************************/ void solve() { auto N = RD(), M = RD(); auto S = RD(), T = RD(); vector s(all(S)), t(all(T)); cout << edit_distance(s, t) ln; }