結果

問題 No.225 文字列変更(medium)
ユーザー giyau02giyau02
提出日時 2020-03-29 16:59:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 947 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 69,176 KB
実行使用メモリ 7,536 KB
最終ジャッジ日時 2023-08-30 19:49:20
合計ジャッジ時間 1,647 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,276 KB
testcase_01 AC 7 ms
7,328 KB
testcase_02 AC 4 ms
7,276 KB
testcase_03 AC 5 ms
7,420 KB
testcase_04 AC 5 ms
7,344 KB
testcase_05 AC 4 ms
7,300 KB
testcase_06 AC 4 ms
7,388 KB
testcase_07 AC 5 ms
7,320 KB
testcase_08 AC 4 ms
7,328 KB
testcase_09 AC 4 ms
7,336 KB
testcase_10 AC 5 ms
7,272 KB
testcase_11 AC 4 ms
7,368 KB
testcase_12 AC 9 ms
7,284 KB
testcase_13 AC 9 ms
7,340 KB
testcase_14 AC 9 ms
7,536 KB
testcase_15 AC 8 ms
7,284 KB
testcase_16 AC 9 ms
7,288 KB
testcase_17 AC 8 ms
7,396 KB
testcase_18 AC 8 ms
7,328 KB
testcase_19 AC 9 ms
7,276 KB
testcase_20 AC 8 ms
7,408 KB
testcase_21 AC 8 ms
7,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int INF = 1<<29; // enough large value

// input
int n, m;
string S, T;

// DP table
int dp[1010][1010];

int main() {
    cin >> n >> m >> S >> T;

    // initialize
    for (int i = 0; i < 1010; ++i) for (int j = 0; j < 1010; ++j) dp[i][j] = INF;
    dp[0][0] = 0;

    for (int i = -1; i < (int)S.size(); ++i) {
        for (int j = -1; j < (int)T.size(); ++j) {
            if (i == -1 && j == -1) continue; // dp[0][0] has been already considered
            if (i >= 0 && j >= 0) {
                if (S[i] == T[j]) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
                else dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + 1);
            }
            if (i >= 0) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j+1] + 1);
            if (j >= 0) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i+1][j] + 1);
        }
    }

    cout << dp[S.size()][T.size()] << endl;
}
0