結果

問題 No.422 文字列変更 (Hard)
ユーザー Sebastian KingSebastian King
提出日時 2023-12-02 23:15:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 3,000 ms
コード長 2,012 bytes
コンパイル時間 3,766 ms
コンパイル使用メモリ 203,308 KB
実行使用メモリ 26,848 KB
最終ジャッジ日時 2023-12-02 23:16:09
合計ジャッジ時間 5,088 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 11 ms
24,800 KB
testcase_02 AC 12 ms
24,800 KB
testcase_03 AC 10 ms
24,760 KB
testcase_04 AC 11 ms
24,800 KB
testcase_05 AC 12 ms
24,800 KB
testcase_06 AC 13 ms
26,848 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 6 ms
26,324 KB
testcase_09 AC 13 ms
26,848 KB
testcase_10 AC 13 ms
26,848 KB
testcase_11 AC 11 ms
24,800 KB
testcase_12 AC 10 ms
24,748 KB
testcase_13 AC 10 ms
24,772 KB
testcase_14 AC 11 ms
24,788 KB
testcase_15 AC 10 ms
24,792 KB
testcase_16 AC 11 ms
24,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int MAXN = 1234;
int f[MAXN][MAXN][4];
const int INF = 1e9;

int n, m;
char s[MAXN], t[MAXN];
char p[MAXN * 2], q[MAXN * 2];

void solve(int casi) {
    scanf("%d%d", &n, &m);
    scanf("%s", s + 1);
    scanf("%s", t + 1);
    f[0][0][0] = f[0][0][3] = 0; f[0][0][1] = f[0][0][2] = INF;
    for (int i = 0; i <= n; ++i) {
        for (int j = (i == 0); j <= m; ++j) {
            // s[i] && t[j]
            if (i > 0 && j > 0) f[i][j][0] = f[i - 1][j - 1][3] + (s[i] != t[j]) * 5;  else f[i][j][0] = INF;
            // s[i] && -
            if (i > 0) f[i][j][1] = min(f[i - 1][j][3] + 9, f[i - 1][j][1] + 2); else f[i][j][1] = INF;
            // - && t[j]
            if (j > 0) f[i][j][2] = min(f[i][j - 1][3] + 9, f[i][j - 1][2] + 2); else f[i][j][2] = INF;
            f[i][j][3] = min({f[i][j][0], f[i][j][1], f[i][j][2]});
        }
    }
    int ans = min({f[n][m][0], f[n][m][1], f[n][m][2]});
    int i = n, j = m, k = (ans == f[n][m][0]) ? 0 : (ans == f[n][m][1] ? 1 : 2), l = 0;
    while (i > 0 || j > 0) {
        if (k == 0) {
            int o = f[i][j][k] - (s[i] != t[j]) * 5;
            p[l] = s[i], q[l] = t[j];
            i--; j--;
            k = (f[i][j][0] == o) ? 0 : (f[i][j][1] == o ? 1 : 2);
        } else if (k == 1) {
            int o = f[i][j][k];
            p[l] = s[i], q[l] = '-';
            i--;
            k = (f[i][j][0] + 9 == o) ? 0 : (f[i][j][1] + 2 == o ? 1 : 2);
        } else {
            int o = f[i][j][k];
            p[l] = '-', q[l] = t[j];
            j--;
            k = (f[i][j][0] + 9 == o) ? 0 : (f[i][j][1] + 9 == o ? 1 : 2);
        }
        l++;
    }
    printf("%d\n", ans);
    for (int i = l - 1; i >= 0; --i) {
        putchar(p[i]);
    }
    puts("");
    for (int i = l - 1; i >= 0; --i) {
        putchar(q[i]);
    }
    puts("");
}

int main(){
    int T = 1;
    for (int i = 1; i <= T; i++)
        solve(i);
    return 0;
}
0