結果

問題 No.225 文字列変更(medium)
ユーザー ふーらくたるふーらくたる
提出日時 2016-09-23 18:18:45
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 814 bytes
コンパイル時間 492 ms
コンパイル使用メモリ 56,152 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-12-24 13:03:57
合計ジャッジ時間 1,353 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

const int MAX_N = 1010;
const int MAX_M = 1010;

const int INF = (1 << 28);

int dp[MAX_N][MAX_M];

int main() {
    int n, m;
    string s, t;
    cin >> n >> m;
    cin >> s >> t;

    fill((int* )dp, (int* )(dp + MAX_N), INF);
    dp[0][0] = 0; dp[n][0] = n; dp[0][m] = m;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (i - 1 >= 0) {
                dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
            }
            if (j - 1 >= 0) {
                dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
            }
            if (i - 1 >= 0 && j - 1 >= 0) {
                dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (s[i - 1] == t[j - 1] ? 0 : 1));
            }
        }
    }

    cout << dp[n][m] << endl;
    return 0;
}
0