#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // ascending order #define vsort(v) sort(v.begin(), v.end()) // descending order #define vsort_r(v) sort(v.begin(), v.end(), greater()) #define vunique(v) unique(v.begin(), v.end()) #define mp make_pair #define ts(x) to_string(x) #define rep(i, a, b) for(int i = (int)a; i < (int)b; i++) #define repm(i, a, b) for(int i = (int)a; i > (int)b; i--) #define bit(a) bitset<8>(a) #define des_priority_queue priority_queue, greater > #define all(v) (v).begin(), (v).end() typedef long long ll; typedef pair P; const ll INF = 1e18; int main(){ cin.tie(0); ios::sync_with_stdio(false); // dp[i][j] : i, j番目まででの最小コスト // dp[i + 1][j + 1] = dp[i][j] + c[i][j] // dp[i + 1][j] + c[i][j] // dp[i][j + 1] + c[i][j] int n, m; cin >> n >> m; string S, T; cin >> S >> T; int s_length = S.size(); int t_length = T.size(); int dp[s_length + 1][t_length + 1]; fill((int*)dp, (int*)(dp + s_length + 1), 1e9); dp[0][0] = 0; rep(i, 0, s_length) rep(j, 0, t_length) { if(S[i] == T[j]) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], min(dp[i][j], min(dp[i + 1][j] + 1, dp[i][j + 1] + 1))); } else { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], min(dp[i][j] + 1, min(dp[i + 1][j] + 1, dp[i][j + 1] + 1))); } } cout << dp[s_length][t_length] << endl; }