結果

問題 No.225 文字列変更(medium)
ユーザー yumarimoyumarimo
提出日時 2019-03-13 23:32:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 1,490 bytes
コンパイル時間 1,397 ms
コンパイル使用メモリ 146,244 KB
実行使用メモリ 7,320 KB
最終ジャッジ日時 2023-09-06 15:26:48
合計ジャッジ時間 2,828 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,900 KB
testcase_01 AC 5 ms
5,864 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 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 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 6 ms
6,820 KB
testcase_13 AC 6 ms
7,320 KB
testcase_14 AC 6 ms
6,996 KB
testcase_15 AC 7 ms
6,988 KB
testcase_16 AC 7 ms
6,888 KB
testcase_17 AC 7 ms
6,864 KB
testcase_18 AC 5 ms
6,724 KB
testcase_19 AC 6 ms
6,876 KB
testcase_20 AC 6 ms
6,664 KB
testcase_21 AC 7 ms
7,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define llong long long int
#define ldouble long double
#define fore(i, x) for (auto &i : n)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n; i >= 0; --i)
#define stl_rep(itr, x) for (auto itr = x.begin(); itr != x.end(); ++itr)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const static int mod = 1000000000 + 7;
const static int inf = INT_MAX / 2;
const static llong INF = LLONG_MAX / 2;
const static double eps = 1e-6;
const static int dx[] = {1, 0, -1, 0};
const static int dy[] = {0, 1, 0, -1};

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1;} return 0;}

template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1;} return 0;}

int levenshtein_distance(string S1, string S2) {
    int l1 = S1.size(), l2 = S2.size();

    int dp[l1 + 1][l2 + 1];
    rep(i, l1 + 1) dp[i][0] = i;
    rep(i, l2 + 1) dp[0][i] = i;

    for (int i = 1; i < l1 + 1; ++i) {
        for (int j = 1; j < l2 + 1; ++j) {
            int alpha = (S1[i - 1] == S2[j - 1]) ? 0 : 1;
            dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + alpha));
        }
    }

    return dp[l1][l2];
}

signed main (int argc, char *argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, m;
    cin >> n >> m;
    string s1, s2;
    cin >> s1 >> s2;
    cout << levenshtein_distance(s1, s2) << endl;
    
    return 0;
}
0