結果

問題 No.225 文字列変更(medium)
ユーザー rvrstonrvrston
提出日時 2020-12-14 23:58:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 810 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 207,112 KB
実行使用メモリ 7,488 KB
最終ジャッジ日時 2023-10-20 05:17:20
合計ジャッジ時間 3,407 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,184 KB
testcase_01 AC 7 ms
6,244 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 10 ms
7,204 KB
testcase_13 AC 10 ms
7,488 KB
testcase_14 AC 10 ms
7,388 KB
testcase_15 AC 9 ms
7,220 KB
testcase_16 AC 10 ms
7,280 KB
testcase_17 AC 9 ms
7,240 KB
testcase_18 AC 9 ms
7,120 KB
testcase_19 AC 9 ms
7,248 KB
testcase_20 AC 9 ms
7,052 KB
testcase_21 AC 10 ms
7,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
  int N1,N2;
  cin >> N1 >> N2;

  string S1,S2;
  cin >> S1 >> S2;

  vector dp(N1+ 1, vector(N2+ 1, 0)); // dp[i][j]= S1[0,i), S2[0,j)に対する編集距離
  iota(dp.at(0).begin(), dp.at(0).end(), 0);
  for(int i= 1; i<= N1; i++){
    dp.at(i).at(0)= i;
    for(int j=1; j<= N2; j++){
      int Creplace= (S1.at(i-1)==S2.at(j-1)) ? dp.at(i-1).at(j-1)
                                             : dp.at(i-1).at(j-1)+ 1;
                                        // S1[i-1]をS2[j-1]に書き換え
      int Cdelete= dp.at(i-1).at(j)+ 1; // S1[i-1]を削除
      int Cinsert= dp.at(i).at(j-1)+ 1; // S1[0,i)の末尾にS2[j-1]を挿入
      dp.at(i).at(j)= min(Creplace, min(Cinsert, Cdelete));
    }
  }
  
  cout << dp.back().back() << endl;
}
0