結果

問題 No.422 文字列変更 (Hard)
ユーザー pekempeypekempey
提出日時 2016-09-09 23:19:16
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,333 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 169,876 KB
実行使用メモリ 138,112 KB
最終ジャッジ日時 2024-04-28 14:06:23
合計ジャッジ時間 4,013 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
138,112 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 121 ms
137,856 KB
testcase_10 AC 123 ms
137,984 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T1, class T2>
bool chmin(T1 &a, T2 b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}

int main() {
	int n, m;
	cin >> n >> m;
	string s, t;
	cin >> s >> t;

	static int dp[1313][1313][2][2]; // i, j, insert, erase
	static tuple<int, int, int, int> pre[1313][1313][2][2]; // i, j, insert, erase

	fill_n(***dp, 1313 * 1313 * 2 * 2, 1e9);
	fill_n(***pre, 1313 * 1313 * 2 * 2, make_tuple(-1, -1, -1, -1));


	for (int i = 0; i <= n; i++) dp[i][0][0][0] = i;
	for (int i = 0; i <= m; i++) dp[0][i][0][0] = i;

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			// replace
			int cost = s[i - 1] == t[j - 1] ? 0 : 5;
			for (int x = 0; x < 2; x++) {
				for (int y = 0; y < 2; y++) {
					if (chmin(dp[i][j][0][0], dp[i - 1][j - 1][x][y] + cost)) {
						pre[i][j][0][0] = make_tuple(i - 1, j - 1, x, y);
					}
				}
			}

			// insert
			if (chmin(dp[i][j][1][0], dp[i - 1][j][0][0] + 9)) pre[i][j][1][0] = make_tuple(i - 1, j, 0, 0);
			if (chmin(dp[i][j][1][0], dp[i - 1][j][0][1] + 9)) pre[i][j][1][0] = make_tuple(i - 1, j, 0, 1);
			if (chmin(dp[i][j][1][0], dp[i - 1][j][1][0] + 2)) pre[i][j][1][0] = make_tuple(i - 1, j, 1, 0);

			// erase
			if (chmin(dp[i][j][0][1], dp[i][j - 1][0][0] + 9)) pre[i][j][0][1] = make_tuple(i, j - 1, 0, 0);
			if (chmin(dp[i][j][0][1], dp[i][j - 1][1][0] + 9)) pre[i][j][0][1] = make_tuple(i, j - 1, 1, 0);
			if (chmin(dp[i][j][0][1], dp[i][j - 1][0][1] + 2)) pre[i][j][0][1] = make_tuple(i, j - 1, 0, 1);
		}
	}

	tuple<int, int, int, int> curr;
	int ans = 1e9;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			if (chmin(ans, dp[n][m][i][j])) {
				curr = make_tuple(n, m, i, j);
			}
		}
	}

	string ss, tt;

	while (true) {
		int i, j, k, l;
		tie(i, j, k, l) = curr;

		auto next = pre[i][j][k][l];

		int ii, jj, kk, ll;
		tie(ii, jj, kk, ll) = next;

		if (ii == -1) break;

		// replace
		if (ii + 1 == i && jj + 1 == j) {
			ss += s[i - 1];
			tt += t[j - 1];
		}

		// insert
		if (ii + 1 == i && jj == j) {
			ss += s[i - 1];
			tt += '-';
		}

		// erase
		if (ii == i && jj + 1 == j) {
			ss += '-';
			tt += t[j - 1];
		}

		curr = next;
	}

	reverse(ss.begin(), ss.end());
	reverse(tt.begin(), tt.end());

	cout << ans << endl;
	cout << ss << endl;
	cout << tt << endl;
}
0