結果

問題 No.422 文字列変更 (Hard)
ユーザー pekempeypekempey
提出日時 2016-09-09 23:29:19
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,652 bytes
コンパイル時間 1,425 ms
コンパイル使用メモリ 171,336 KB
実行使用メモリ 138,220 KB
最終ジャッジ日時 2024-04-28 14:07:11
合計ジャッジ時間 3,529 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
138,056 KB
testcase_01 AC 71 ms
138,192 KB
testcase_02 AC 74 ms
138,192 KB
testcase_03 AC 69 ms
138,188 KB
testcase_04 AC 71 ms
138,192 KB
testcase_05 AC 75 ms
138,196 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 73 ms
138,196 KB
testcase_10 AC 75 ms
138,196 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 69 ms
138,216 KB
testcase_14 AC 75 ms
138,088 KB
testcase_15 AC 70 ms
138,192 KB
testcase_16 AC 72 ms
137,940 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 = 0; i <= n; i++) {
		if (i == 1) {
			dp[i][0][0][1] = 9;
		} else if (i > 1) {
			dp[i][0][0][1] = 9 + 2 * (i - 1);
		}
	}
	dp[0][0][0][0] = 0;
	for (int i = 0; i <= m; i++) {
		if (i == 1) {
			dp[0][i][1][0] = 9;
		} else if (i > 1) {
			dp[0][i][1][0] = 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) {
				tt += '-';
				i--;
			}

			if (j > 0) {
				ss += '-';
				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