結果

問題 No.225 文字列変更(medium)
ユーザー crossr0ad
提出日時 2022-03-17 21:25:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 4,868 ms
コンパイル使用メモリ 197,500 KB
最終ジャッジ日時 2025-01-28 10:05:07
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

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