結果

問題 No.225 文字列変更(medium)
ユーザー airisairis
提出日時 2015-06-12 23:24:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 1,221 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 75,872 KB
実行使用メモリ 11,540 KB
最終ジャッジ日時 2023-08-25 22:46:29
合計ジャッジ時間 1,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
11,316 KB
testcase_01 AC 18 ms
11,376 KB
testcase_02 AC 5 ms
11,176 KB
testcase_03 AC 5 ms
11,296 KB
testcase_04 AC 5 ms
11,300 KB
testcase_05 AC 5 ms
11,196 KB
testcase_06 AC 6 ms
11,272 KB
testcase_07 AC 5 ms
11,196 KB
testcase_08 AC 6 ms
11,272 KB
testcase_09 AC 6 ms
11,192 KB
testcase_10 AC 6 ms
11,240 KB
testcase_11 AC 5 ms
11,328 KB
testcase_12 AC 21 ms
11,260 KB
testcase_13 AC 23 ms
11,404 KB
testcase_14 AC 23 ms
11,440 KB
testcase_15 AC 15 ms
11,408 KB
testcase_16 AC 23 ms
11,360 KB
testcase_17 AC 14 ms
11,280 KB
testcase_18 AC 19 ms
11,540 KB
testcase_19 AC 23 ms
11,272 KB
testcase_20 AC 20 ms
11,252 KB
testcase_21 AC 23 ms
11,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <numeric>
#include <bitset>
#include <complex>
#define rep(x, to) for (int x = 0; x < (to); x++)
#define REP(x, a, to) for (int x = (a); x < (to); x++)
#define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++)
#define EPS (1e-14)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;

int n, m;
string s, t;

ll memo[1005][1005];

ll dfs(int i, int j) {
	ll res = 0;
	if (i >= n) return m - j;
	if (j >= m) return n - i;
	if (memo[i][j] != -1) return memo[i][j];
	if (s[i] == t[j]) {
		res = dfs(i + 1, j + 1);
	}
	else {
		ll res1 = dfs(i + 1, j + 1) + 1LL; //変換
		ll res2 = dfs(i + 1, j) + 1LL;//削除
		ll res3 = dfs(i, j + 1) + 1LL;//挿入
		res = min(res1, min(res2, res3));
	}
	//printf("dfs(%d,%d)=%d\n", i, j, res);
	return memo[i][j] = res;
}

void solve() {
	memset(memo, -1, sizeof(memo));
	cout << dfs(0, 0) << endl;
}

int main() {
	cin >> n >> m;
	cin >> s;
	cin >> t;
	solve();
	return 0;
}


0