結果

問題 No.225 文字列変更(medium)
ユーザー Yang33Yang33
提出日時 2018-08-23 23:57:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 2,705 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 169,744 KB
実行使用メモリ 6,792 KB
最終ジャッジ日時 2023-08-28 15:25:33
合計ジャッジ時間 2,563 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,612 KB
testcase_01 AC 6 ms
5,668 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,504 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 7 ms
6,528 KB
testcase_13 AC 7 ms
6,760 KB
testcase_14 AC 7 ms
6,792 KB
testcase_15 AC 7 ms
6,468 KB
testcase_16 AC 7 ms
6,768 KB
testcase_17 AC 7 ms
6,464 KB
testcase_18 AC 7 ms
6,524 KB
testcase_19 AC 7 ms
6,468 KB
testcase_20 AC 7 ms
6,516 KB
testcase_21 AC 6 ms
6,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/23  Problem: yukicoder 225  / Link: http://yukicoder.me/problems/no/225  ----- */
/* ------問題------

長さnの文字列Sと長さmの文字列Tが与えられます。文字列を構成する各文字は、'a'~'z'です。
今、Sに以下の3種類の操作のいずれかを順次実施してTに変換したいです。
そのような一連の操作のうち、操作回数の最小となるものについて、その最小値を出力するプログラムを書いて下さい。

(操作)
【変更】 Sの中から文字S[ i ]を1個選んで、その文字を'a'~'z'のいずれかの好きな文字に変更します。
【削除】 Sの中から文字S[ i ]を1個選んで、その文字を削除します。削除によって空文字列になることも許容します。
【挿入】 Sの好きな箇所に、'a'~'z'のいずれかの文字を挿入します。特に、Sの先頭や最後尾に文字を追加することもできます。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

int LevenshteinDistance(const string& str1, const string& str2){
	int lenstr1 = str1.length() + 1;
	int lenstr2 = str2.length() + 1;
	vector<vector<int>> d(lenstr1+1,vector<int>(lenstr2+1,0));
	int i1 = 0, i2 = 0, cost = 0;

	for (; i1 < lenstr1; i1++) d[i1][0] = i1;
	for (; i2 < lenstr2; i2++) d[0][i2] = i2;

	for (i1 = 1; i1 < lenstr1; i1++) {
		for (i2 = 1; i2 < lenstr2; i2++) {
			cost = str1[i1 - 1] == str2[i2 - 1] ? 0 : 1;
			d[i1][i2] = min({ d[i1 - 1][i2] + 1, d[i1][i2 - 1] + 1, d[i1 - 1][i2 - 1] + cost });
		}
	}

	return d[lenstr1 - 1][lenstr2 - 1];
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int N; cin >> N >> N;
	string s, t; cin >> s >> t;
	cout << LevenshteinDistance(s, t) << "\n";

	return 0;
}
0