結果

問題 No.225 文字列変更(medium)
ユーザー crossr0adcrossr0ad
提出日時 2022-03-17 21:25:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 200,052 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 20:09:23
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 7 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 10 ms
6,944 KB
testcase_13 AC 10 ms
6,940 KB
testcase_14 AC 10 ms
6,940 KB
testcase_15 AC 11 ms
6,940 KB
testcase_16 AC 10 ms
6,944 KB
testcase_17 AC 10 ms
6,944 KB
testcase_18 AC 10 ms
6,940 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 10 ms
6,940 KB
testcase_21 AC 10 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <class T>
inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

const int INF = 1 << 29;

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

    int N, M;
    cin >> N >> M;
    string S, T;
    cin >> S >> T;

    vector<vector<int>> dp(N + 1, vector<int>(M + 1, INF));
    // dp[i][j] := edit distance between S[:i] and T[:j]

    dp[0][0] = 0;

    for (int i = 0; i <= N; ++i) {
        for (int j = 0; j <= M; ++j) {
            // modify
            if (i > 0 && j > 0) {
                if (S[i - 1] == T[j - 1]) {
                    chmin(dp[i][j], dp[i - 1][j - 1]);
                } else {
                    chmin(dp[i][j], dp[i - 1][j - 1] + 1);
                }
            }

            // erase
            if (i > 0) {
                chmin(dp[i][j], dp[i - 1][j] + 1);
            }

            // insert
            if (j > 0) {
                chmin(dp[i][j], dp[i][j - 1] + 1);
            }
        }
    }

    cout << dp[N][M] << endl;
}
0