結果

問題 No.225 文字列変更(medium)
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-10-07 13:50:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 859 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 61,764 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2023-08-26 00:42:08
合計ジャッジ時間 1,923 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,492 KB
testcase_01 AC 7 ms
7,400 KB
testcase_02 AC 5 ms
7,588 KB
testcase_03 AC 5 ms
7,404 KB
testcase_04 AC 5 ms
7,412 KB
testcase_05 AC 5 ms
7,404 KB
testcase_06 AC 4 ms
7,448 KB
testcase_07 AC 5 ms
7,608 KB
testcase_08 AC 5 ms
7,384 KB
testcase_09 AC 5 ms
7,564 KB
testcase_10 AC 5 ms
7,392 KB
testcase_11 AC 5 ms
7,412 KB
testcase_12 AC 7 ms
7,596 KB
testcase_13 AC 8 ms
7,420 KB
testcase_14 AC 7 ms
7,388 KB
testcase_15 AC 7 ms
7,676 KB
testcase_16 AC 7 ms
7,584 KB
testcase_17 AC 7 ms
7,412 KB
testcase_18 AC 7 ms
7,632 KB
testcase_19 AC 8 ms
7,404 KB
testcase_20 AC 7 ms
7,404 KB
testcase_21 AC 8 ms
7,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;

//dp[i][j] := 文字列Sのi番目までと文字列Tのj番目までを考えた時の、
//操作回数の最小値
int dp[1010][1010];

int main(void){
	int n, m; cin >> n >> m;
	string S, T; cin >> S >> T;
	rep(i, 1010)rep(j, 1010)dp[i][j] = INF;
	//片方が0の時は挿入をする
	rep(i, 1010) dp[i][0] = dp[0][i] = i;

	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= m; ++j){
			if(S[i - 1] == T[j - 1]){//i文字目とj文字目は一致
				dp[i][j] = min({dp[i - 1][j - 1], dp[i - 1][j] + 1, dp[i][j - 1] + 1});
			}else{
				dp[i][j] = min({dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1});				
			}
		}
	}
	printf("%d\n", dp[n][m]);
	return 0;
}
0