using System; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Diagnostics; using Enu = System.Linq.Enumerable; public class Program { static readonly int INF = (int)1e9; static readonly int Replace = 5; static readonly int FirstAddDelete = 9; static readonly int AddDelete = 2; public void Solve() { int N = Reader.Int(), M = Reader.Int(); var S = Reader.String(); var T = Reader.String(); Func Cost = len => 9 + (len - 1) * 2; var minCost = new int[N + 1, M + 1, 3]; var prevSi = new int[N + 1, M + 1, 3]; var prevTi = new int[N + 1, M + 1, 3]; var prevType = new int[N + 1, M + 1, 3]; Fill(minCost, INF); minCost[0, 0, 0] = 0; for (int si = 0; si <= N; si++) for (int ti = 0; ti <= M; ti++) for (int type = 0; type < 3 && (si < N || ti < M); type++) { Action Update = (nsi, nti, ntype, cost) => { if (cost >= minCost[nsi, nti, ntype]) return; minCost[nsi, nti, ntype] = cost; prevSi[nsi, nti, ntype] = si; prevTi[nsi, nti, ntype] = ti; prevType[nsi, nti, ntype] = type; }; int currCost = minCost[si, ti, type]; if (si < N) { // replace if (ti < M) { if (si < N) Update(si + 1, ti + 1, 0, currCost + (S[si] == T[ti] ? 0 : Replace)); } // delete Update(si + 1, ti, 2, currCost + FirstAddDelete); if (type == 2) Update(si + 1, ti, 2, currCost + AddDelete); } if (ti < M) { // add Update(si, ti + 1, 1, currCost + FirstAddDelete); if (type == 1) Update(si, ti + 1, 1, currCost + AddDelete); } } int ans = int.MaxValue, Si = N, Ti = M, AnsType = 0; for (int type = 0; type < 3; type++) if (minCost[N, M, type] < ans) { ans = minCost[N, M, type]; AnsType = type; } var ansS = new StringBuilder(); var ansT = new StringBuilder(); while (Si > 0 || Ti > 0) { int nsi = prevSi[Si, Ti, AnsType]; int nti = prevTi[Si, Ti, AnsType]; int ntype = prevType[Si, Ti, AnsType]; switch (AnsType) { case 0: ansS.Insert(0, S[nsi]); ansT.Insert(0, T[nti]); break; case 1: ansS.Insert(0, "-"); ansT.Insert(0, T[nti]); break; case 2: ansS.Insert(0, S[nsi]); ansT.Insert(0, "-"); break; } Si = nsi; Ti = nti; AnsType = ntype; } Console.WriteLine(ans); Console.WriteLine(ansS); Console.WriteLine(ansT); Console.ReadLine(); } void Fill(T[, ,] X, T val) { int A = X.GetLength(0), B = X.GetLength(1), C = X.GetLength(2); for (int a = 0; a < A; a++) for (int b = 0; b < B; b++) for (int c = 0; c < C; c++) X[a, b, c] = val; } } class Entry { static void Main() { new Program().Solve(); } } class Reader { static TextReader reader = Console.In; static readonly char[] separator = { ' ' }; static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries; static string[] A = new string[0]; static int i; static void Init() { A = new string[0]; } public static void Set(TextReader r) { reader = r; Init(); } public static void Set(string file) { reader = new StreamReader(file); Init(); } public static bool HasNext() { return CheckNext(); } public static string String() { return Next(); } public static int Int() { return int.Parse(Next()); } public static long Long() { return long.Parse(Next()); } public static double Double() { return double.Parse(Next()); } public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); } public static int[] IntArray(int N) { return Range(N, Int); } public static int[][] IntTable(int H) { return Range(H, IntLine); } public static string[] StringArray(int N) { return Range(N, Next); } public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); } public static string Line() { return reader.ReadLine().Trim(); } static string[] Split(string s) { return s.Split(separator, op); } static T[] Range(int N, Func f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; } static string Next() { CheckNext(); return A[i++]; } static bool CheckNext() { if (i < A.Length) return true; string line = reader.ReadLine(); if (line == null) return false; if (line == "") return CheckNext(); A = Split(line); i = 0; return true; } }