結果

問題 No.422 文字列変更 (Hard)
ユーザー Sebastian KingSebastian King
提出日時 2023-12-02 23:11:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 3,000 ms
コード長 2,200 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 203,436 KB
実行使用メモリ 42,988 KB
最終ジャッジ日時 2023-12-02 23:11:22
合計ジャッジ時間 3,365 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 17 ms
36,760 KB
testcase_02 AC 17 ms
36,844 KB
testcase_03 AC 15 ms
36,760 KB
testcase_04 AC 16 ms
36,760 KB
testcase_05 AC 17 ms
36,844 KB
testcase_06 AC 20 ms
42,988 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 9 ms
42,652 KB
testcase_09 AC 20 ms
40,940 KB
testcase_10 AC 21 ms
42,988 KB
testcase_11 AC 16 ms
36,760 KB
testcase_12 AC 15 ms
36,760 KB
testcase_13 AC 15 ms
36,760 KB
testcase_14 AC 15 ms
36,760 KB
testcase_15 AC 15 ms
36,760 KB
testcase_16 AC 16 ms
36,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

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

const int MAXN = 2048;
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);
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            for (int k = 0; k < 3; ++k) {
                f[i][j][k] = INF;
            }
        }
    }
    f[0][0][0] = 0;
    // for (int j = 0; j < 3; ++j) f[0][0][j] = 0;
    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] = min({f[i - 1][j - 1][0], f[i - 1][j - 1][1], f[i - 1][j - 1][2]}) + (s[i] != t[j]) * 5;
            // s[i] && -
            if (i > 0)
                f[i][j][1] = min({f[i - 1][j][0] + 9, f[i - 1][j][1] + 2, f[i - 1][j][2] + 9});
            // - && t[j]
            if (j > 0)
                f[i][j][2] = min({f[i][j - 1][0] + 9, f[i][j - 1][1] + 9, f[i][j - 1][2] + 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