結果

問題 No.225 文字列変更(medium)
ユーザー MisterMister
提出日時 2020-09-04 17:11:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 955 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 76,436 KB
実行使用メモリ 6,776 KB
最終ジャッジ日時 2023-08-17 10:33:37
合計ジャッジ時間 1,682 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,748 KB
testcase_01 AC 6 ms
5,648 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 8 ms
6,492 KB
testcase_13 AC 8 ms
6,772 KB
testcase_14 AC 8 ms
6,776 KB
testcase_15 AC 7 ms
6,508 KB
testcase_16 AC 8 ms
6,768 KB
testcase_17 AC 7 ms
6,456 KB
testcase_18 AC 7 ms
6,428 KB
testcase_19 AC 7 ms
6,432 KB
testcase_20 AC 8 ms
6,500 KB
testcase_21 AC 7 ms
6,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

constexpr int INF = 1 << 30;

void solve() {
    int n, m;
    std::string s, t;
    std::cin >> n >> m >> s >> t;

    auto dp = vec(n + 1, vec(m + 1, INF));
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            if (i == 0 || j == 0) dp[i][j] = i + j;

            if (i + 1 <= n) dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + 1);
            if (j + 1 <= m) dp[i][j + 1] = std::min(dp[i][j + 1], dp[i][j] + 1);
            if (i + 1 <= n && j + 1 <= m) {
                dp[i + 1][j + 1] = std::min(dp[i + 1][j + 1],
                                            dp[i][j] + (s[i] != t[j]));
            }
        }
    }

    std::cout << dp[n][m] << "\n";
}

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

    solve();

    return 0;
}
0