#include #include using namespace std; const int INF = 1<<30; const int BUF = 1205; enum Type { MATCH = 0, DELETE = 1, INSERT = 2 }; class Next { public: int type, aPt, bPt; Next(){} Next(int type, int aPt, int bPt): type(type), aPt(aPt), bPt(bPt){} }; int aLen, bLen; string a, b; void read() { cin >> aLen >> bLen; cin >> a >> b; } void rec(int preType, int aPos, int bPos, int dp[3][BUF][BUF], Next next[3][BUF][BUF]) { int &ret = dp[preType][aPos][bPos]; Next &nex = next[preType][aPos][bPos]; if (ret != -1) return; if (aPos == aLen && bPos == bLen) { ret = 0; return; } ret = INF; // match if (aPos < aLen && bPos < bLen) { rec(MATCH, aPos + 1, bPos + 1, dp, next); if (ret > dp[MATCH][aPos + 1][bPos + 1] + (a[aPos] == b[bPos] ? 0 : 5)) { ret = dp[MATCH][aPos + 1][bPos + 1] + (a[aPos] == b[bPos] ? 0 : 5); nex = Next(MATCH, aPos + 1, bPos + 1); } } // delete if (aPos < aLen) { rec(DELETE, aPos + 1, bPos, dp, next); if (ret > (preType == DELETE ? 2 : 9) + dp[DELETE][aPos + 1][bPos]) { ret = (preType == DELETE ? 2 : 9) + dp[DELETE][aPos + 1][bPos]; nex = Next(DELETE, aPos + 1, bPos); } } // insert if (bPos < bLen) { rec(INSERT, aPos, bPos + 1, dp, next); if (ret > (preType == INSERT ? 2 : 9) + dp[INSERT][aPos][bPos + 1]) { ret = (preType == INSERT ? 2 : 9) + dp[INSERT][aPos][bPos + 1]; nex = Next(INSERT, aPos, bPos + 1); } } } void work() { static int dp[3][BUF][BUF]; static Next next[3][BUF][BUF]; memset(dp, -1, sizeof(dp)); rec(MATCH, 0, 0, dp, next); cout << dp[MATCH][0][0] << endl; int preType = MATCH; int aPos = 0; int bPos = 0; string aAns; string bAns; while (!(aPos == aLen && bPos == bLen)) { Next &nex = next[preType][aPos][bPos]; switch (nex.type) { case MATCH: aAns += a[aPos]; bAns += b[bPos]; break; case DELETE: aAns += a[aPos]; bAns += '-'; break; case INSERT: aAns += '-'; bAns += b[bPos]; break; } preType = nex.type; aPos = nex.aPt; bPos = nex.bPt; } cout << aAns << endl; cout << bAns << endl; } int main() { read(); work(); return 0; }