結果

問題 No.422 文字列変更 (Hard)
ユーザー femtofemto
提出日時 2016-09-09 23:52:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,771 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 41,856 KB
最終ジャッジ日時 2024-04-28 14:10:19
合計ジャッジ時間 2,215 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
23,424 KB
testcase_01 AC 34 ms
38,656 KB
testcase_02 AC 36 ms
38,656 KB
testcase_03 AC 32 ms
37,888 KB
testcase_04 AC 34 ms
38,528 KB
testcase_05 AC 38 ms
38,656 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 35 ms
40,832 KB
testcase_10 AC 38 ms
41,856 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 33 ms
38,272 KB
testcase_14 AC 32 ms
38,528 KB
testcase_15 AC 33 ms
38,656 KB
testcase_16 AC 35 ms
38,784 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(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(ni != 0 && nj != 0) {
		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--;
		}
	}
	cout << s << endl;
	cout << t << endl;
}
0