結果

問題 No.225 文字列変更(medium)
ユーザー oshamashamaoshamashama
提出日時 2020-07-28 05:56:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 889 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 68,792 KB
実行使用メモリ 7,488 KB
最終ジャッジ日時 2023-09-11 06:19:23
合計ジャッジ時間 1,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
7,416 KB
testcase_01 AC 8 ms
7,300 KB
testcase_02 AC 4 ms
7,300 KB
testcase_03 AC 4 ms
7,364 KB
testcase_04 AC 4 ms
7,348 KB
testcase_05 AC 5 ms
7,340 KB
testcase_06 AC 5 ms
7,336 KB
testcase_07 AC 4 ms
7,460 KB
testcase_08 AC 4 ms
7,344 KB
testcase_09 AC 4 ms
7,488 KB
testcase_10 AC 4 ms
7,396 KB
testcase_11 AC 4 ms
7,368 KB
testcase_12 AC 9 ms
7,376 KB
testcase_13 AC 9 ms
7,376 KB
testcase_14 AC 9 ms
7,296 KB
testcase_15 AC 9 ms
7,352 KB
testcase_16 AC 9 ms
7,320 KB
testcase_17 AC 9 ms
7,416 KB
testcase_18 AC 8 ms
7,428 KB
testcase_19 AC 9 ms
7,360 KB
testcase_20 AC 8 ms
7,400 KB
testcase_21 AC 9 ms
7,300 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