結果

問題 No.225 文字列変更(medium)
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-21 11:38:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 1,031 bytes
コンパイル時間 1,542 ms
コンパイル使用メモリ 166,164 KB
実行使用メモリ 7,564 KB
最終ジャッジ日時 2023-09-27 10:51:02
合計ジャッジ時間 3,021 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,540 KB
testcase_01 AC 7 ms
7,368 KB
testcase_02 AC 4 ms
7,392 KB
testcase_03 AC 5 ms
7,408 KB
testcase_04 AC 5 ms
7,376 KB
testcase_05 AC 5 ms
7,524 KB
testcase_06 AC 4 ms
7,564 KB
testcase_07 AC 5 ms
7,380 KB
testcase_08 AC 5 ms
7,368 KB
testcase_09 AC 4 ms
7,428 KB
testcase_10 AC 5 ms
7,380 KB
testcase_11 AC 4 ms
7,364 KB
testcase_12 AC 8 ms
7,328 KB
testcase_13 AC 8 ms
7,372 KB
testcase_14 AC 9 ms
7,368 KB
testcase_15 AC 8 ms
7,376 KB
testcase_16 AC 8 ms
7,428 KB
testcase_17 AC 8 ms
7,368 KB
testcase_18 AC 8 ms
7,484 KB
testcase_19 AC 8 ms
7,372 KB
testcase_20 AC 8 ms
7,536 KB
testcase_21 AC 8 ms
7,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




int N, M; string S, T;
//-----------------------------------------------------------------------------------
#define INF INT_MAX/2
int dp[1010][1010];
int main() {
    cin >> N >> M >> S >> T;

    rep(i, 0, 1010) rep(j, 0, 1010) dp[i][j] = INF;
    dp[0][0] = 0;

    rep(i, 0, N + 1) rep(j, 0, M + 1) {
        if (i == N && j == M) continue;
        if (i == N) dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
        else if (j == M) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
        else {
            // nothing
            if (S[i] == T[j]) {
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
            }

            // change
            dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);

            // erase
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);

            // insert
            dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
        }
    }

    printf("%d\n", dp[N][M]);
}
0