#include using namespace std; typedef long long ll; typedef pair PII; const int MAXN = 1234; int f[MAXN][MAXN][4]; const int INF = 1e9; int n, m; char s[MAXN], t[MAXN]; char p[MAXN * 2], q[MAXN * 2]; void solve(int casi) { scanf("%d%d", &n, &m); scanf("%s", s + 1); scanf("%s", t + 1); f[0][0][0] = f[0][0][3] = 0; f[0][0][1] = f[0][0][2] = INF; for (int i = 0; i <= n; ++i) { for (int j = (i == 0); j <= m; ++j) { // s[i] && t[j] if (i > 0 && j > 0) f[i][j][0] = f[i - 1][j - 1][3] + (s[i] != t[j]) * 5; else f[i][j][0] = INF; // s[i] && - if (i > 0) f[i][j][1] = min(f[i - 1][j][3] + 9, f[i - 1][j][1] + 2); else f[i][j][1] = INF; // - && t[j] if (j > 0) f[i][j][2] = min(f[i][j - 1][3] + 9, f[i][j - 1][2] + 2); else f[i][j][2] = INF; f[i][j][3] = min({f[i][j][0], f[i][j][1], f[i][j][2]}); } } int ans = min({f[n][m][0], f[n][m][1], f[n][m][2]}); int i = n, j = m, k = (ans == f[n][m][0]) ? 0 : (ans == f[n][m][1] ? 1 : 2), l = 0; while (i > 0 || j > 0) { if (k == 0) { int o = f[i][j][k] - (s[i] != t[j]) * 5; p[l] = s[i], q[l] = t[j]; i--; j--; k = (f[i][j][0] == o) ? 0 : (f[i][j][1] == o ? 1 : 2); } else if (k == 1) { int o = f[i][j][k]; p[l] = s[i], q[l] = '-'; i--; k = (f[i][j][0] + 9 == o) ? 0 : (f[i][j][1] + 2 == o ? 1 : 2); } else { int o = f[i][j][k]; p[l] = '-', q[l] = t[j]; j--; k = (f[i][j][0] + 9 == o) ? 0 : (f[i][j][1] + 9 == o ? 1 : 2); } l++; } printf("%d\n", ans); for (int i = l - 1; i >= 0; --i) { putchar(p[i]); } puts(""); for (int i = l - 1; i >= 0; --i) { putchar(q[i]); } puts(""); } int main(){ int T = 1; for (int i = 1; i <= T; i++) solve(i); return 0; }