結果

問題 No.225 文字列変更(medium)
ユーザー arrowsarrows
提出日時 2017-01-23 11:21:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 737 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 65,624 KB
実行使用メモリ 7,072 KB
最終ジャッジ日時 2023-08-26 00:44:00
合計ジャッジ時間 1,553 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>

using namespace std;

int levenshtein_distance(string& s, string& t,
			 int incost = 1, int rmcost = 1, int recost = 1)
{
    int N = s.size(), M = t.size();
    int dp[N + 1][M + 1];

    for (int i = 0; i <= N; i++) {
	dp[i][0] = i * incost;
    }
    for (int i = 0; i <= M; i++) {
	dp[0][i] = i * rmcost;
    }
    
    for (int i = 1; i <= N; i++) {
	for (int j = 1; j <= M; j++) {
	    int rcost = (s[i - 1] == t[j - 1] ? 0 : recost);
	    dp[i][j] = min(dp[i - 1][j] + incost,
			   min(dp[i][j - 1] + rmcost, dp[i - 1][j - 1] + rcost));
	}
    }
    return dp[N][M];
}

int main()
{
    int N, M;
    string S, T;
    cin >> N >> M >> S >> T;
    cout << levenshtein_distance(S, T) << endl;
    return 0;
}
0