結果

問題 No.422 文字列変更 (Hard)
ユーザー antaanta
提出日時 2016-09-09 22:44:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 3,000 ms
コード長 2,145 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 173,560 KB
実行使用メモリ 70,820 KB
最終ジャッジ日時 2023-08-10 20:55:18
合計ジャッジ時間 3,555 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 51 ms
49,352 KB
testcase_02 AC 60 ms
54,304 KB
testcase_03 AC 49 ms
45,336 KB
testcase_04 AC 53 ms
49,052 KB
testcase_05 AC 62 ms
57,012 KB
testcase_06 AC 57 ms
70,820 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 59 ms
64,792 KB
testcase_10 AC 63 ms
68,436 KB
testcase_11 AC 55 ms
50,044 KB
testcase_12 AC 48 ms
44,596 KB
testcase_13 AC 50 ms
46,548 KB
testcase_14 AC 53 ms
47,632 KB
testcase_15 AC 52 ms
48,084 KB
testcase_16 AC 55 ms
49,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

typedef pair<int, pii> P;
struct DP {
	vector<P> dp;
	int n, m;
	DP(int n, int m) : n(n), m(m), dp((n + 1) * (m + 1) * 4, P{ INF,{-1,-1} }) {}
	P &operator()(int i, int j, int lasti, int lastj) {
		return dp[(i * (m + 1) + j) * 4 + lasti * 2 + lastj];
	}
};

int main() {
	int n; int m;
	while(~scanf("%d%d", &n, &m)) {
		char S[1201], T[1201];
		scanf("%s", S);
		scanf("%s", T);
		DP dp(n, m);
		dp(0, 0, 0, 0) = { 0, {-1, -1} };
		rep(i, n + 1) rep(j, m + 1) rep(lasti, 2) rep(lastj, 2) {
			int x = dp(i, j, lasti, lastj).first;
			if(x == INF) continue;
			pair<int, int> p(i * 2 + lasti, j * 2 + lastj);
			if(i < n && j < m)
				amin(dp(i + 1, j + 1, 0, 0), P{ x + (S[i] == T[j] ? 0 : 5), p });
			if(i < n)
				amin(dp(i + 1, j, 1, 0), P{ x + (lasti == 0 ? 9 : 2), p });
			if(j < m)
				amin(dp(i, j + 1, 0, 1), P{ x + (lastj == 0 ? 9 : 2), p });
		}
		pair<int, pii> ans{ INF,{} };
		rep(lasti, 2) rep(lastj, 2)
			amin(ans, make_pair(dp(n, m, lasti, lastj).first, make_pair(lasti, lastj)));
		printf("%d\n", ans.first);
		string alignment[2];
		{
			int i = n, j = m, lasti = ans.second.first, lastj = ans.second.second;
			while(i != 0 || j != 0) {
				int pi, pj;
				tie(pi, pj) = dp(i, j, lasti, lastj).second;
				int ni = pi / 2, nj = pj / 2;
				alignment[0] += ni + 1 == i ? S[ni] : '-';
				alignment[1] += nj + 1 == j ? T[nj] : '-';
				i = ni, j = nj;
				lasti = pi % 2, lastj = pj % 2;
			}
			rep(k, 2)
				reverse(alignment[k].begin(), alignment[k].end());
		}
		puts(alignment[0].c_str());
		puts(alignment[1].c_str());
	}
	return 0;
}
0