結果

問題 No.225 文字列変更(medium)
ユーザー liktastarliktastar
提出日時 2020-11-04 23:12:25
言語 Ruby
(3.3.0)
結果
AC  
実行時間 799 ms / 5,000 ms
コード長 651 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 11,188 KB
実行使用メモリ 28,592 KB
最終ジャッジ日時 2023-09-29 16:22:46
合計ジャッジ時間 11,044 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 360 ms
20,372 KB
testcase_01 AC 563 ms
25,380 KB
testcase_02 AC 77 ms
15,104 KB
testcase_03 AC 75 ms
15,116 KB
testcase_04 AC 76 ms
15,156 KB
testcase_05 AC 76 ms
15,040 KB
testcase_06 AC 79 ms
15,128 KB
testcase_07 AC 78 ms
15,052 KB
testcase_08 AC 77 ms
15,084 KB
testcase_09 AC 77 ms
15,268 KB
testcase_10 AC 78 ms
15,108 KB
testcase_11 AC 76 ms
15,240 KB
testcase_12 AC 747 ms
27,920 KB
testcase_13 AC 799 ms
28,468 KB
testcase_14 AC 782 ms
28,592 KB
testcase_15 AC 754 ms
28,116 KB
testcase_16 AC 760 ms
28,580 KB
testcase_17 AC 752 ms
27,712 KB
testcase_18 AC 731 ms
27,664 KB
testcase_19 AC 756 ms
28,176 KB
testcase_20 AC 718 ms
27,656 KB
testcase_21 AC 755 ms
28,096 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Main.rb:1: warning: assigned but unused variable - m
Syntax OK

ソースコード

diff #

n, m = gets.chomp.split(" ").map(&:to_i)
s = gets.chomp
t = gets.chomp

INF = 10 ** 9
dp = []
(s.length + 1).times do |i|
  dp[i] = []
  (t.length + 1).times do |j|
    dp[i][j] = INF
  end
end

dp[0][0] = 0
(s.length + 1).times do |i|
  (t.length + 1).times do |j|
    # 変更
    if i > 0 && j > 0
      if s[i - 1] == t[j - 1]
        dp[i][j] = [dp[i][j], dp[i-1][j-1]].min
      else
        dp[i][j] = [dp[i][j], dp[i-1][j-1] + 1].min
      end
    end
    # 削除
    if i > 0
      dp[i][j] = [dp[i][j], dp[i-1][j] + 1].min
    end
    # 挿入
    if j > 0
      dp[i][j] = [dp[i][j], dp[i][j-1] + 1].min
    end
  end
end

puts dp[-1][-1]
0