#include using namespace std; const int INF = 1000000; int dp[1201][1201][3][3]; int min(int a[3]) { return min(min(a[0], a[1]), a[2]); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; string s, t; cin >> n >> m >> s >> t; for (int i = 1; i < 3; i++) { for (int j = 0; j < 3; j++) { dp[0][0][i][j] = INF; } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { dp[i][0][0][j] = INF; dp[i][0][1][j] = 9 + 2 * (i - 1); dp[i][0][2][j] = INF; } } for (int i = 1; i <= m; i++) { for (int j = 0; j < 3; j++) { dp[0][i][0][j] = INF; dp[0][i][1][j] = INF; dp[0][i][2][j] = 9 + 2 * (i - 1); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k < 3; k++) { int c_sub, c_ins, c_del; if (s[i - 1] == t[j - 1]) c_sub = 0; else c_sub = 5; if (k == 1) c_ins = 2; else c_ins = 9; if (k == 2) c_del = 2; else c_del = 9; dp[i][j][0][k] = min(INF, min(dp[i - 1][j - 1][k]) + c_sub); dp[i][j][1][k] = min(INF, min(dp[i - 1][j][k]) + c_ins); dp[i][j][2][k] = min(INF, min(dp[i][j - 1][k]) + c_del); } } } int cost = INF, now = 0; for (int i = 0; i < 3; i++) { int tmp = min(dp[n][m][i]); if (tmp < cost) { cost = tmp; now = i; } } int idx_s = n, idx_t = m; string ali_s = "", ali_t = ""; while (idx_s > 0 || idx_t > 0) { int tmp; int past; switch(now) { case 0: ali_s = s[idx_s - 1] + ali_s; ali_t = t[idx_t - 1] + ali_t; idx_s--; idx_t--; tmp = (s[idx_s] == t[idx_t]?0:5); for (int i = 0; i <= 2; i++) { if (dp[idx_s + 1][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; case 2: ali_s = "-" + ali_s; ali_t = t[idx_t - 1] + ali_t; idx_t--; for (int i = 0; i <= 2; i++) { tmp = (now == i?2:9); if (dp[idx_s][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; case 1: ali_s = s[idx_s - 1] + ali_s; ali_t = "-" + ali_t; idx_s--; for (int i = 0; i <= 2; i++) { tmp = (now == i?2:9); if (dp[idx_s][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; } now = past; } cout << cost << "\n"; cout << ali_s << "\n"; cout << ali_t << "\n"; return 0; }