import java.util.Scanner; public class Y422 { Y422() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); char[] S = sc.next().toCharArray(); char[] T = sc.next().toCharArray(); int[][][][] dp = new int[n+1][m+1][2][2]; int[][][][][] from = new int[n+1][m+1][2][2][]; for (int i = 0; i <= S.length; i++) { for (int j = 0; j <= T.length; j++) { for (int a = 0; a < 2; a ++) { for (int b = 0; b < 2; b ++ ) { dp[i][j][a][b] = 100 * (n + m); } } } } dp[0][0][0][0] = 0; for (int i = 0; i < S.length; i++) { for (int j = 0; j < T.length; j++) { int change_cost = 5; if (S[i] == T[j]) change_cost = 0; int cost = change_cost + dp[i][j][0][0]; { int[] tmp = { i, j, 0, 0 }; dp[i+1][j+1][0][0] = cost; from[i+1][j+1][0][0] = tmp; } cost = change_cost + dp[i][j][0][1]; if (cost < dp[i+1][j+1][0][0]) { int[] tmp = { i, j, 0, 1 }; dp[i+1][j+1][0][0] = cost; from[i+1][j+1][0][0] = tmp; } cost = change_cost + dp[i][j][1][0]; if (cost < dp[i+1][j+1][0][0]) { int[] tmp = { i, j, 1, 0 }; dp[i+1][j+1][0][0] = cost; from[i+1][j+1][0][0] = tmp; } cost = 9 + dp[i+1][j][0][0]; { int[] tmp = { i+1, j, 0, 0 }; dp[i+1][j+1][1][0] = cost; from[i+1][j+1][1][0] = tmp; } cost = 2 + dp[i+1][j][1][0]; if (cost < dp[i+1][j+1][1][0]) { int[] tmp = { i+1, j, 1, 0 }; dp[i+1][j+1][1][0] = cost; from[i+1][j+1][1][0] = tmp; } cost = 9 + dp[i+1][j][0][1]; if (cost < dp[i+1][j+1][1][0]) { int[] tmp = { i+1, j, 0, 1 }; dp[i+1][j+1][1][0] = cost; from[i+1][j+1][1][0] = tmp; } cost = 9 + dp[i][j+1][0][0]; { int[] tmp = { i, j+1, 0, 0 }; dp[i+1][j+1][0][1] = cost; from[i+1][j+1][0][1] = tmp; } cost = 9 + dp[i][j+1][1][0]; if (cost < dp[i+1][j+1][0][1]) { int[] tmp = { i, j+1, 1, 0 }; dp[i+1][j+1][0][1] = cost; from[i+1][j+1][0][1] = tmp; } cost = 2 + dp[i][j+1][0][1]; if (cost < dp[i+1][j+1][0][1]) { int[] tmp = { i, j+1, 0, 1 }; dp[i+1][j+1][0][1] = cost; from[i+1][j+1][0][1] = tmp; } } } int cost = 100 * (n + m); int[] state = null; for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { if (cost > dp[S.length][T.length][a][b]) { cost = dp[S.length][T.length][a][b]; state = from[S.length][T.length][a][b]; } } } char[] S_ans = new char[n+m]; char[] T_ans = new char[n+m]; int len = 0; while (state != null) { if (state[2] == 1) { S_ans[len] = '-'; } else { S_ans[len] = S[state[0]]; } if (state[3] == 1) { T_ans[len] = '-'; } else { T_ans[len] = T[state[1]]; } len++; state = from[state[0]][state[1]][state[2]][state[3]]; } System.out.println(cost); for (int i = len - 1; i >= 0; i--) { System.out.print(S_ans[i]); } System.out.println(""); for (int i = len - 1; i >= 0; i--) { System.out.print(T_ans[i]); } System.out.println(""); } public static void main(String argv[]) { new Y422(); } }