#include #include using namespace std; const int INF = 1<<30; const int BUF = 1205; class Next { public: int aPos, bPos; int aStart, bStart; int len; Next(){} Next(int aPos, int bPos, int aStart, int bStart, int len): aPos(aPos), bPos(bPos), aStart(aStart), bStart(bStart), len(len) {} }; int aLen, bLen; string a, b; void read() { cin >> aLen >> bLen; cin >> a >> b; } void rec(int aPos, int bPos, int dp[BUF][BUF], Next next[BUF][BUF]) { int &ret = dp[aPos][bPos]; Next &nex = next[aPos][bPos]; if (ret != -1) return; if (aPos == aLen) { ret = bLen == bPos ? 0 : 9 + 2 * (bLen - bPos - 1); nex = Next(aLen, bLen, -1, bLen, 0); return; } if (bPos == bLen) { ret = aLen == aPos ? 0 : 9 + 2 * (aLen - aPos - 1); nex = Next(aLen, bLen, aLen, -1, 0); return; } ret = INF; for (int bStart = bPos; bStart < bLen; ++bStart) { int cost = bStart == bPos ? 0 : 9 + 2 * (bStart - bPos - 1); for (int len = 1; aPos + len <= aLen && bStart + len <= bLen; ++len) { cost += a[aPos + len - 1] == b[bStart + len - 1] ? 0 : 5; rec(aPos + len, bStart + len, dp, next); if (ret > cost + dp[aPos + len][bStart + len]) { ret = cost + dp[aPos + len][bStart + len]; nex = Next(aPos + len, bStart + len, -1, bStart, len); } } } for (int aStart = aPos; aStart < aLen; ++aStart) { int cost = aStart == aPos ? 0 : 9 + 2 * (aStart - aPos - 1); for (int len = 1; bPos + len <= bLen && aStart + len <= aLen; ++len) { cost += b[bPos + len - 1] == a[aStart + len - 1] ? 0 : 5; rec(aStart + len, bPos + len, dp, next); if (ret > cost + dp[aStart + len][bPos + len]) { ret = cost + dp[aStart + len][bPos + len]; nex = Next(aStart + len, bPos + len, aStart, -1, len); } } } } void work() { static int dp[BUF][BUF]; static Next next[BUF][BUF]; memset(dp, -1, sizeof(dp)); rec(0, 0, dp, next); cout << dp[0][0] << endl; int aPos = 0; int bPos = 0; string aAns; string bAns; while (!(aPos == aLen && bPos == bLen)) { Next &nex = next[aPos][bPos]; if (nex.aStart != -1) { for (int i = aPos; i < nex.aStart; ++i) { aAns += a[i]; bAns += '-'; } aPos = nex.aStart; } for (int i = aPos; i < aPos + nex.len; ++i) { aAns += a[i]; } if (nex.bStart != -1) { for (int i = bPos; i < nex.bStart; ++i) { aAns += '-'; bAns += b[i]; } bPos = nex.bStart; } for (int i = bPos; i < bPos + nex.len; ++i) { bAns += b[i]; } aPos = nex.aPos; bPos = nex.bPos; } cout << aAns << endl; cout << bAns << endl; } int main() { read(); work(); return 0; }