結果

問題 No.225 文字列変更(medium)
ユーザー rogi52rogi52
提出日時 2022-10-17 03:08:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 983 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 202,392 KB
実行使用メモリ 6,988 KB
最終ジャッジ日時 2023-09-10 04:25:11
合計ジャッジ時間 4,136 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,648 KB
testcase_01 AC 5 ms
5,664 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 7 ms
6,668 KB
testcase_13 AC 8 ms
6,816 KB
testcase_14 AC 8 ms
6,988 KB
testcase_15 AC 7 ms
6,644 KB
testcase_16 AC 7 ms
6,812 KB
testcase_17 AC 8 ms
6,488 KB
testcase_18 AC 7 ms
6,488 KB
testcase_19 AC 7 ms
6,484 KB
testcase_20 AC 7 ms
6,484 KB
testcase_21 AC 8 ms
6,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int weighted_levenshtein(string &a, string &b, int _insert = 1, int _delete = 1, int _substitute = 1){
    int len_a = a.size(), len_b = b.size();
    vector<vector<int>> dp(len_a + 1, vector<int> (len_b + 1, 0));
    for(int i = 0; i < len_a + 1; i++) dp[i][0] = i * _delete;
    for(int j = 0; j < len_b + 1; j++) dp[0][j] = j * _insert;
    for(int i = 1; i < len_a + 1; i++){
        for(int j = 1; j < len_b + 1; j++){
            int x = (a[i - 1] == b[j - 1] ? 0 : _substitute);
            dp[i][j] = min({dp[i - 1][j] + _delete,
                            dp[i][j - 1] + _insert,
                            dp[i - 1][j - 1] + x});
       }
    }
    return dp[len_a][len_b];
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n,m; cin >> n >> m;
    string S,T; cin >> S >> T;
    cout << weighted_levenshtein(S, T, 1, 1, 1) << "\n";
}
0