結果

問題 No.225 文字列変更(medium)
ユーザー fukunagaS4fukunagaS4
提出日時 2020-04-21 11:00:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 874 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 194,880 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-04-16 18:40:13
合計ジャッジ時間 3,032 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,376 KB
testcase_01 AC 8 ms
7,680 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 11 ms
7,296 KB
testcase_13 AC 10 ms
7,424 KB
testcase_14 AC 10 ms
7,632 KB
testcase_15 AC 10 ms
7,424 KB
testcase_16 AC 11 ms
7,556 KB
testcase_17 AC 10 ms
7,276 KB
testcase_18 AC 10 ms
7,204 KB
testcase_19 AC 11 ms
7,552 KB
testcase_20 AC 11 ms
7,376 KB
testcase_21 AC 10 ms
7,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

const int INF = 1 << 29;

// dp := 文字列sのi番目を変換して文字列tのj番目までと等しくするまでの最小操作回数
int dp[1010][1010];

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	char s[1010], t[1010];
	scanf("%s%s", s, t);
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++)
			dp[i][j] = INF;
	dp[0][0] = 0;
	for(int i = -1; i < n; i++) {
		for(int j = -1; j < m; j++) {
			if(i == -1 && j == -1) continue;
			if(i >= 0 && j >= 0) {
				if(s[i] == t[j]) chmin(dp[i+1][j+1], dp[i][j]);
				else chmin(dp[i+1][j+1], dp[i][j] + 1);
			}
			if(j >= 0) chmin(dp[i+1][j+1], dp[i+1][j] + 1);
			if(i >= 0) chmin(dp[i+1][j+1], dp[i][j+1] + 1);
		}
	}
	printf("%d\n", dp[n][m]);
}
0