結果

問題 No.225 文字列変更(medium)
ユーザー kyunakyuna
提出日時 2019-08-21 21:01:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 806 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 76,172 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-04-17 23:42:02
合計ジャッジ時間 1,736 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 6 ms
5,888 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 7 ms
6,784 KB
testcase_13 AC 7 ms
7,040 KB
testcase_14 AC 7 ms
6,940 KB
testcase_15 AC 7 ms
6,656 KB
testcase_16 AC 7 ms
6,912 KB
testcase_17 AC 6 ms
6,912 KB
testcase_18 AC 7 ms
6,656 KB
testcase_19 AC 7 ms
6,912 KB
testcase_20 AC 7 ms
6,528 KB
testcase_21 AC 7 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = (int)1e9 + 7;

int levenshtein_distance(string s, string t) {
    int m = s.length(), n = t.length();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, INF));
    for (int i = 0; i <= m; i++) dp[i][0] = i;
    for (int j = 0; j <= n; j++) dp[0][j] = j;
    for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) {
        dp[i + 1][j + 1] = min({dp[i][j] + (s[i] != t[j]),  // substitution
                                dp[i][j + 1] + 1,           // insertion
                                dp[i + 1][j] + 1});         // deletion
    }
    return dp[m][n];
}

int main() {
    int n, m; cin >> n >> m;
    string s, t;
    cin >> s >> t;
    cout << levenshtein_distance(s, t) << endl;
    return 0;
}
0