#include using namespace std; template bool chmin(T1 &a, T2 b) { if (b < a) { a = b; return true; } return false; } int main() { int n, m; cin >> n >> m; string s, t; cin >> s >> t; static int dp[1313][1313][2][2]; // i, j, insert, erase static tuple pre[1313][1313][2][2]; // i, j, insert, erase fill_n(***dp, 1313 * 1313 * 2 * 2, 1e9); fill_n(***pre, 1313 * 1313 * 2 * 2, make_tuple(-1, -1, -1, -1)); dp[0][0][0][0] = 0; for (int i = 0; i <= n; i++) { if (i == 1) { dp[i][0][0][1] = 9; } else if (i > 1) { dp[i][0][0][1] = 9 + 2 * (i - 1); } } dp[0][0][0][0] = 0; for (int i = 0; i <= m; i++) { if (i == 1) { dp[0][i][1][0] = 9; } else if (i > 1) { dp[0][i][1][0] = 9 + 2 * (i - 1); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { // replace int cost = s[i - 1] == t[j - 1] ? 0 : 5; for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { if (chmin(dp[i][j][0][0], dp[i - 1][j - 1][x][y] + cost)) { pre[i][j][0][0] = make_tuple(i - 1, j - 1, x, y); } } } // insert if (chmin(dp[i][j][1][0], dp[i - 1][j][0][0] + 9)) pre[i][j][1][0] = make_tuple(i - 1, j, 0, 0); if (chmin(dp[i][j][1][0], dp[i - 1][j][0][1] + 9)) pre[i][j][1][0] = make_tuple(i - 1, j, 0, 1); if (chmin(dp[i][j][1][0], dp[i - 1][j][1][0] + 2)) pre[i][j][1][0] = make_tuple(i - 1, j, 1, 0); // erase if (chmin(dp[i][j][0][1], dp[i][j - 1][0][0] + 9)) pre[i][j][0][1] = make_tuple(i, j - 1, 0, 0); if (chmin(dp[i][j][0][1], dp[i][j - 1][1][0] + 9)) pre[i][j][0][1] = make_tuple(i, j - 1, 1, 0); if (chmin(dp[i][j][0][1], dp[i][j - 1][0][1] + 2)) pre[i][j][0][1] = make_tuple(i, j - 1, 0, 1); } } tuple curr; int ans = 1e9; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (chmin(ans, dp[n][m][i][j])) { curr = make_tuple(n, m, i, j); } } } string ss, tt; while (true) { int i, j, k, l; tie(i, j, k, l) = curr; auto next = pre[i][j][k][l]; int ii, jj, kk, ll; tie(ii, jj, kk, ll) = next; if (ii == -1) { while (i > 0) { tt += '-'; i--; } if (j > 0) { ss += '-'; j--; } break; } // replace if (ii + 1 == i && jj + 1 == j) { ss += s[i - 1]; tt += t[j - 1]; } // insert if (ii + 1 == i && jj == j) { ss += s[i - 1]; tt += '-'; } // erase if (ii == i && jj + 1 == j) { ss += '-'; tt += t[j - 1]; } curr = next; } reverse(ss.begin(), ss.end()); reverse(tt.begin(), tt.end()); cout << ans << endl; cout << ss << endl; cout << tt << endl; }