結果

問題 No.422 文字列変更 (Hard)
ユーザー hipokabahipokaba
提出日時 2016-09-12 04:45:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 3,000 ms
コード長 1,494 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 62,824 KB
実行使用メモリ 20,284 KB
最終ジャッジ日時 2023-08-10 21:10:44
合計ジャッジ時間 1,656 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 11 ms
17,812 KB
testcase_02 AC 11 ms
17,732 KB
testcase_03 AC 10 ms
17,684 KB
testcase_04 AC 10 ms
17,924 KB
testcase_05 AC 11 ms
17,720 KB
testcase_06 AC 14 ms
20,284 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 5 ms
18,444 KB
testcase_09 AC 13 ms
19,760 KB
testcase_10 AC 13 ms
20,252 KB
testcase_11 AC 10 ms
17,692 KB
testcase_12 AC 10 ms
17,676 KB
testcase_13 AC 10 ms
17,908 KB
testcase_14 AC 10 ms
17,676 KB
testcase_15 AC 10 ms
17,944 KB
testcase_16 AC 10 ms
17,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

const int inf = 1e5;

int n, m;
string s, t;
string u, v;

int dp[1201][1201][3];

void calc(){
	dp[0][0][0] = 0;
	dp[0][0][1] = dp[0][0][2] = inf;
	for(int j = 1; j <= m; ++j){
		dp[0][j][0] = dp[0][j][2] = 9 + (j - 1) * 2;
		dp[0][j][1] = inf;
	}
	for(int i = 1; i <= n; ++i){
		dp[i][0][0] = dp[i][0][1] = 9 + (i - 1) * 2;
		dp[i][0][2] = inf;
		for(int j = 1; j <= m; ++j){
			dp[i][j][1] = min(
				dp[i - 1][j][0] + 9,
				dp[i - 1][j][1] + 2
			);
			dp[i][j][2] = min(
				dp[i][j - 1][0] + 9,
				dp[i][j - 1][2] + 2
			);
			dp[i][j][0] = min(
				dp[i - 1][j - 1][0] + (s[i - 1] == t[j - 1] ? 0 : 5),
				min(dp[i][j][1], dp[i][j][2])
			);
		}
	}
}

void restore(int i, int j, int k){
	if(i == 0 || j == 0){
		while(i > 0){
			u += s[--i];
			v += '-';
		}
		while(j > 0){
			u += '-';
			v += t[--j];
		}	
		return;
	}
	int p = dp[i][j][k];
	if(k == 1 || k == 0 && p == dp[i][j][1]){
		u += s[i - 1];
		v += '-';
		restore(i - 1, j, p == dp[i - 1][j][1] + 2);
	}
	else if(k == 2 || k == 0 && p == dp[i][j][2]){
		u += '-';
		v += t[j - 1];
		restore(i, j - 1, p == dp[i][j - 1][2] + 2 ? 2 : 0);
	}
	else{
		u += s[i - 1];
		v += t[j - 1];
		restore(i - 1, j - 1, 0);
	}
}

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

	calc();

	restore(n, m, 0);
	reverse(u.begin(), u.end());
	reverse(v.begin(), v.end());
	cout << dp[n][m][0] << endl << u << endl << v << endl;
	return 0;
}
0