結果

問題 No.422 文字列変更 (Hard)
ユーザー femtofemto
提出日時 2016-09-10 00:00:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 3,000 ms
コード長 1,845 bytes
コンパイル時間 3,341 ms
コンパイル使用メモリ 80,772 KB
実行使用メモリ 43,212 KB
最終ジャッジ日時 2023-08-10 21:02:36
合計ジャッジ時間 2,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
43,048 KB
testcase_01 AC 40 ms
43,060 KB
testcase_02 AC 45 ms
43,156 KB
testcase_03 AC 37 ms
43,176 KB
testcase_04 AC 40 ms
43,080 KB
testcase_05 AC 45 ms
43,076 KB
testcase_06 AC 35 ms
43,104 KB
testcase_07 AC 14 ms
43,212 KB
testcase_08 AC 13 ms
43,096 KB
testcase_09 AC 40 ms
43,088 KB
testcase_10 AC 44 ms
43,124 KB
testcase_11 AC 40 ms
43,076 KB
testcase_12 AC 38 ms
43,072 KB
testcase_13 AC 38 ms
43,100 KB
testcase_14 AC 38 ms
43,100 KB
testcase_15 AC 38 ms
43,088 KB
testcase_16 AC 40 ms
43,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cassert>
using namespace std;
const int INF = 1 << 28;

// 0:default, 1:delete, 2:insert
int dp[1300][1300][3];
// 0:replace, 1:delete: 2:insert
int pre[1300][1300][3];


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;

	string S, T;
	cin >> S >> T;

	fill((int*)begin(pre), (int*)end(pre), -1);
	fill((int*)begin(dp), (int*)end(dp), INF);
	dp[0][0][0] = 0;
	for(int i = 0; i <= n; i++) {
		for(int j = 0; j <= m; j++) {
			for(int k = 0; k < 3; k++) {
				if(dp[i][j][k] == INF) continue;
				int v = dp[i][j][k], nv;
				// replace
				if(i < n && j < m) {
					nv = v + ((S[i] == T[j]) ? 0 : 5);
					if(dp[i + 1][j + 1][0] > nv) {
						dp[i + 1][j + 1][0] = nv;
						pre[i + 1][j + 1][0] = k;
					}
				}
				// delete
				if(i < n) {
					nv = v + ((k == 1) ? 2 : 9);
					if(dp[i + 1][j][1] > nv) {
						dp[i + 1][j][1] = nv;
						pre[i + 1][j][1] = k;
					}
				}

				// insert
				if(j < m) {
					nv = v + ((k == 2) ? 2 : 9);
					if(dp[i][j + 1][2] > nv) {
						dp[i][j + 1][2] = nv;
						pre[i][j + 1][2] = k;
					}
				}
			}
		}
	}

	int  mink = -1, minc = INF;
	for(int k = 0; k < 3; k++) {
		if(dp[n][m][k] < minc) {
			minc = dp[n][m][k];
			mink = k;
		}
	}
	assert(mink != -1);

	cout << minc << endl;
	int ni = n, nj = m, nk = mink;
	string s, t;
	while(true) {
		if(nk == 0) {
			nk = pre[ni][nj][nk];
			s = S[ni - 1] + s;
			t = T[nj - 1] + t;
			ni--, nj--;

		}
		else if(nk == 1) {
			nk = pre[ni][nj][nk];
			s = S[ni - 1] + s;
			t = '-' + t;
			ni--;
		}
		else {
			nk = pre[ni][nj][nk];
			s = '-' + s;
			t = T[nj - 1] + t;
			nj--;
		}
		if(ni == 0 && nj == 0 && nk == 0) break;
	}
	cout << s << endl;
	cout << t << endl;
}
0