結果

問題 No.422 文字列変更 (Hard)
ユーザー pekempeypekempey
提出日時 2016-09-09 23:41:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 3,000 ms
コード長 2,542 bytes
コンパイル時間 1,534 ms
コンパイル使用メモリ 157,668 KB
実行使用メモリ 138,348 KB
最終ジャッジ日時 2023-08-10 20:58:29
合計ジャッジ時間 3,502 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
138,040 KB
testcase_01 AC 71 ms
138,088 KB
testcase_02 AC 73 ms
138,128 KB
testcase_03 AC 69 ms
138,348 KB
testcase_04 AC 70 ms
138,052 KB
testcase_05 AC 73 ms
138,088 KB
testcase_06 AC 76 ms
138,096 KB
testcase_07 AC 47 ms
138,076 KB
testcase_08 AC 48 ms
138,076 KB
testcase_09 AC 73 ms
138,044 KB
testcase_10 AC 73 ms
138,128 KB
testcase_11 AC 70 ms
138,088 KB
testcase_12 AC 67 ms
138,064 KB
testcase_13 AC 68 ms
138,104 KB
testcase_14 AC 69 ms
138,348 KB
testcase_15 AC 69 ms
138,212 KB
testcase_16 AC 69 ms
138,052 KB
権限があれば一括ダウンロードができます

ソースコード

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));

	dp[0][0][0][0] = 0;
	for (int i = 1; i <= n; i++) {
		dp[i][0][1][0] = 9 + 2 * (i - 1);
	}
	for (int i = 1; i <= m; i++) {
		dp[0][i][0][1] = 9 + 2 * (i - 1);
	}

	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) {
			while (i > 0) {
				ss += s[i - 1];
				tt += '-';
				i--;
			}

			while (j > 0) {
				ss += '-';
				tt += t[j - 1];
				j--;
			}

			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