#include #include #include #include #include #include using namespace std; const int INF = 1 << 28; // 0:default, 1:delete, 2:insert int dp[1300][1300][3]; // 0:replace, 1:delete: 2:insert int pre[1300][1300][3]; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; string S, T; cin >> S >> T; fill((int*)begin(pre), (int*)end(pre), -1); fill((int*)begin(dp), (int*)end(dp), INF); dp[0][0][0] = 0; for(int i = 0; i <= n; i++) { for(int j = 0; j <= m; j++) { for(int k = 0; k < 3; k++) { if(dp[i][j][k] == INF) continue; int v = dp[i][j][k], nv; // replace if(i < n && j < m) { nv = v + ((S[i] == T[j]) ? 0 : 5); if(dp[i + 1][j + 1][0] > nv) { dp[i + 1][j + 1][0] = nv; pre[i + 1][j + 1][0] = k; } } // delete if(i < n) { nv = v + ((k == 1) ? 2 : 9); if(dp[i + 1][j][1] > nv) { dp[i + 1][j][1] = nv; pre[i + 1][j][1] = k; } } // insert if(j < m) { nv = v + ((k == 2) ? 2 : 9); if(dp[i][j + 1][2] > nv) { dp[i][j + 1][2] = nv; pre[i][j + 1][2] = k; } } } } } int mink = -1, minc = INF; for(int k = 0; k < 3; k++) { if(dp[n][m][k] < minc) { minc = dp[n][m][k]; mink = k; } } assert(mink != -1); cout << minc << endl; int ni = n, nj = m, nk = mink; string s, t; while(true) { if(nk == 0) { nk = pre[ni][nj][nk]; s = S[ni - 1] + s; t = T[nj - 1] + t; ni--, nj--; } else if(nk == 1) { nk = pre[ni][nj][nk]; s = S[ni - 1] + s; t = '-' + t; ni--; } else { nk = pre[ni][nj][nk]; s = '-' + s; t = T[nj - 1] + t; nj--; } if(ni == 0 && nj == 0 && nk == 0); } cout << s << endl; cout << t << endl; }