結果

問題 No.422 文字列変更 (Hard)
ユーザー square1001square1001
提出日時 2020-03-05 20:32:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,065 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 80,776 KB
実行使用メモリ 82,304 KB
最終ジャッジ日時 2024-04-22 03:16:23
合計ジャッジ時間 2,778 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, M; string S, T;
	cin >> N >> M >> S >> T;
	vector<vector<vector<int> > > dp(N + 1, vector<vector<int> >(M + 1, vector<int>(3, inf)));
	dp[0][0][0] = 0;
	for (int i = 0; i <= N; ++i) {
		for (int j = 0; j <= M; ++j) {
			if (i >= 1 && j >= 1) {
				dp[i][j][0] = min({ dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2] }) + (S[i - 1] == T[j - 1] ? 0 : 5);
			}
			if (i >= 1) {
				dp[i][j][1] = min({ dp[i - 1][j][0] + 9, dp[i - 1][j][1] + 2, dp[i - 1][j][2] + 9 });
			}
			if (j >= 1) {
				dp[i][j][2] = min({ dp[i][j - 1][0] + 9, dp[i][j - 1][1] + 9, dp[i][j - 1][2] + 2 });
			}
		}
	}
	int res = min({ dp[N][M][0], dp[N][M][1], dp[N][M][2] });
	int cx = N, cy = M, cz = (res == dp[N][M][0] ? 0 : (res == dp[N][M][1] ? 1 : 2));
	string SA, TA;
	while (cx != 0 || cy != 0) {
		int cur = dp[cx][cy][cz];
		if (cx >= 1 && cy >= 1) {
			int tar = cur - (S[cx - 1] == T[cy - 1] ? 0 : 5);
			bool f = false;
			if (dp[cx - 1][cy - 1][0] == tar) cz = 0, f = true;
			else if (dp[cx - 1][cy - 1][1] == tar) cz = 1, f = true;
			else if (dp[cx - 1][cy - 1][2] == tar) cz = 2, f = true;
			if (f) {
				SA += S[--cx];
				TA += T[--cy];
				continue;
			}
		}
		if (cx >= 1) {
			bool f = false;
			if (dp[cx - 1][cy][0] == cur - 9) cz = 0, f = true;
			else if (dp[cx - 1][cy][1] == cur - 2) cz = 1, f = true;
			else if (dp[cx - 1][cy][2] == cur - 9) cz = 2, f = true;
			if (f) {
				SA += S[--cx];
				TA += "-";
				continue;
			}
		}
		if (cy >= 1) {
			bool f = false;
			if (dp[cx][cy - 1][0] == cur - 9) cz = 0, f = true;
			else if (dp[cx][cy - 1][1] == cur - 9) cz = 1, f = true;
			else if (dp[cx][cy - 1][2] == cur - 2) cz = 2, f = true;
			if (f) {
				SA += "-";
				TA += T[--cy];
				continue;
			}
		}
	}
	reverse(SA.begin(), SA.end());
	reverse(TA.begin(), TA.end());
	cout << res << endl;
	cout << SA << endl;
	cout << TA << endl;
	return 0;
}
0