#include #include #include #include using namespace std; // 0 : 挿入も削除もしない // 1 : 挿入した // 2 : 削除した int dp[1300][1300][3]; int main() { int n, m; string S, T; cin >> n >> m; cin >> S >> T; for(int i = 0; i <= T.size(); i++) { dp[0][i][0] = 1e9; dp[0][i][1] = 9 + (i-1)*2; dp[0][i][2] = 1e9; } for(int i = 0; i <= S.size(); i++) { dp[i][0][0] = 1e9; dp[i][0][1] = 1e9; dp[i][0][2] = 9 + (i-1)*2; } dp[0][0][0] = 0; dp[0][0][1] = 1e9; dp[0][0][2] = 1e9; for(int i = 0; i < S.size(); i++) { for(int j = 0; j < T.size(); j++) { dp[i+1][j+1][0] = min(min(dp[i][j][0], dp[i][j][1]), dp[i][j][2]) + (S[i]==T[j] ? 0 : 5); dp[i+1][j+1][1] = min(dp[i+1][j][0] + 9, min(dp[i+1][j][1] + 2, dp[i+1][j][2] + 9)); dp[i+1][j+1][2] = min(dp[i][j+1][0] + 9, min(dp[i][j+1][1] + 9, dp[i][j+1][2] + 2)); } } int res = 1e9; int act; for(int i = 0; i < 3; i++) { if(res > dp[n][m][i]) { res = dp[n][m][i]; act = i; } } string rS = "", rT = ""; while(n+m) { if(act == 0) { for(int i = 0; i < 3; i++) { if(dp[n][m][act] == dp[n-1][m-1][i] + (S[n-1] == T[m-1] ? 0 : 5)) { act = i; break; } } n--; m--; rS += S[n]; rT += T[m]; } else if (act == 1){ for(int i = 0; i < 3; i++) { if(dp[n][m][act] == dp[n][m-1][i] + (act == i ? 2 : 9)) { act = i; break; } } m--; rS += "-"; rT += T[m]; } else { for(int i = 0; i < 3; i++) { if(dp[n][m][act] == dp[n-1][m][i] + (act == i ? 2 : 9)) { act = i; break; } } n--; rS += S[n]; rT += "-"; } } reverse(rS.begin(), rS.end()); reverse(rT.begin(), rT.end()); cout << res << endl; cout << rS << endl; cout << rT << endl; }