結果

問題 No.225 文字列変更(medium)
ユーザー drken1215drken1215
提出日時 2017-06-29 15:13:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 880 bytes
コンパイル時間 410 ms
コンパイル使用メモリ 58,336 KB
実行使用メモリ 7,488 KB
最終ジャッジ日時 2024-04-15 04:51:38
合計ジャッジ時間 1,164 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,160 KB
testcase_01 AC 6 ms
7,304 KB
testcase_02 AC 3 ms
7,304 KB
testcase_03 AC 3 ms
7,488 KB
testcase_04 AC 4 ms
7,232 KB
testcase_05 AC 3 ms
7,412 KB
testcase_06 AC 4 ms
7,300 KB
testcase_07 AC 3 ms
7,452 KB
testcase_08 AC 4 ms
7,476 KB
testcase_09 AC 3 ms
7,336 KB
testcase_10 AC 3 ms
7,296 KB
testcase_11 AC 4 ms
7,440 KB
testcase_12 AC 8 ms
7,424 KB
testcase_13 AC 8 ms
7,200 KB
testcase_14 AC 8 ms
7,396 KB
testcase_15 AC 7 ms
7,468 KB
testcase_16 AC 8 ms
7,424 KB
testcase_17 AC 8 ms
7,296 KB
testcase_18 AC 8 ms
7,416 KB
testcase_19 AC 8 ms
7,400 KB
testcase_20 AC 8 ms
7,420 KB
testcase_21 AC 9 ms
7,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int INF = 1<<29;        // 十分大きい値にする

// 入力
int n, m;
string S, T;

// DP テーブル
int dp[1010][1010];

int main() {
  cin >> n >> m >> S >> T;

  // 初期化
  for (int i = 0; i < 1010; ++i) for (int j = 0; j < 1010; ++j) dp[i][j] = INF;
  dp[0][0] = 0;
    
  for (int i = -1; i < (int)S.size(); ++i) {
    for (int j = -1; j < (int)T.size(); ++j) {
      if (i == -1 && j == -1) continue;   // dp[0][0] は考慮済
      if (i >= 0 && j >= 0) {
	if (S[i] == T[j]) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
	else dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + 1);
      }
      if (i >= 0) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j+1] + 1);
      if (j >= 0) dp[i+1][j+1] = min(dp[i+1][j+1], dp[i+1][j] + 1);
    }
  }

  cout << dp[S.size()][T.size()] << endl;
}
0