結果
問題 | No.422 文字列変更 (Hard) |
ユーザー | fine |
提出日時 | 2016-09-25 01:45:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,277 bytes |
コンパイル時間 | 1,929 ms |
コンパイル使用メモリ | 173,752 KB |
実行使用メモリ | 54,228 KB |
最終ジャッジ日時 | 2024-11-18 11:03:18 |
合計ジャッジ時間 | 3,997 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | RE | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | RE | - |
testcase_08 | WA | - |
testcase_09 | AC | 32 ms
51,932 KB |
testcase_10 | WA | - |
testcase_11 | RE | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
ソースコード
#include <bits/stdc++.h> using namespace std; const int INF = 1000000; int dp[1201][1201][3][3]; int min(int a[3]) { return min(min(a[0], a[1]), a[2]); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; string s, t; cin >> n >> m >> s >> t; for (int i = 1; i < 3; i++) { for (int j = 0; j < 3; j++) { dp[0][0][i][j] = INF; } } for (int i = 1; i <= n; i++) { for (int j = 0; j < 3; j++) { dp[i][0][0][j] = INF; dp[i][0][1][j] = 9 + 2 * (i - 1); dp[i][0][2][j] = INF; } } for (int i = 1; i <= m; i++) { for (int j = 0; j < 3; j++) { dp[0][i][0][j] = INF; dp[0][i][1][j] = INF; dp[0][i][2][j] = 9 + 2 * (i - 1); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { for (int k = 0; k < 3; k++) { int c_sub, c_ins, c_del; if (s[i - 1] == t[j - 1]) c_sub = 0; else c_sub = 5; if (k == 1) c_ins = 2; else c_ins = 9; if (k == 2) c_del = 2; else c_del = 9; dp[i][j][0][k] = min(INF, min(dp[i - 1][j - 1][k]) + c_sub); dp[i][j][1][k] = min(INF, min(dp[i - 1][j][k]) + c_ins); dp[i][j][2][k] = min(INF, min(dp[i][j - 1][k]) + c_del); } } } int cost = INF, now = 0; for (int i = 0; i < 3; i++) { int tmp = min(dp[n][m][i]); if (tmp < cost) { cost = tmp; now = i; } } int idx_s = n, idx_t = m; string ali_s = "", ali_t = ""; while (idx_s > 0 || idx_t > 0) { int tmp; int past; switch(now) { case 0: ali_s = s[idx_s - 1] + ali_s; ali_t = t[idx_t - 1] + ali_t; idx_s--; idx_t--; tmp = (s[idx_s] == t[idx_t]?0:5); for (int i = 0; i <= 2; i++) { if (dp[idx_s + 1][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; case 2: ali_s = "-" + ali_s; ali_t = t[idx_t - 1] + ali_t; idx_t--; for (int i = 0; i <= 2; i++) { tmp = (now == i?2:9); if (dp[idx_s][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; case 1: ali_s = s[idx_s - 1] + ali_s; ali_t = "-" + ali_t; idx_s--; for (int i = 0; i <= 2; i++) { tmp = (now == i?2:9); if (dp[idx_s][idx_t + 1][now][i] == min(dp[idx_s][idx_t][i]) + tmp) { past = i; break; } } break; } now = past; } cout << cost << "\n"; cout << ali_s << "\n"; cout << ali_t << "\n"; return 0; }