結果

問題 No.225 文字列変更(medium)
ユーザー shoutmshoutm
提出日時 2019-05-01 13:22:11
言語 Ruby
(3.3.0)
結果
AC  
実行時間 648 ms / 5,000 ms
コード長 582 bytes
コンパイル時間 429 ms
コンパイル使用メモリ 11,240 KB
実行使用メモリ 25,408 KB
最終ジャッジ日時 2023-08-30 04:19:22
合計ジャッジ時間 10,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 297 ms
19,012 KB
testcase_01 AC 459 ms
22,240 KB
testcase_02 AC 80 ms
15,156 KB
testcase_03 AC 80 ms
15,100 KB
testcase_04 AC 82 ms
15,140 KB
testcase_05 AC 82 ms
15,356 KB
testcase_06 AC 80 ms
15,232 KB
testcase_07 AC 81 ms
15,100 KB
testcase_08 AC 82 ms
15,144 KB
testcase_09 AC 83 ms
15,164 KB
testcase_10 AC 83 ms
15,144 KB
testcase_11 AC 82 ms
15,136 KB
testcase_12 AC 604 ms
24,584 KB
testcase_13 AC 648 ms
25,316 KB
testcase_14 AC 633 ms
25,324 KB
testcase_15 AC 604 ms
24,828 KB
testcase_16 AC 620 ms
25,408 KB
testcase_17 AC 612 ms
24,572 KB
testcase_18 AC 595 ms
24,720 KB
testcase_19 AC 618 ms
25,048 KB
testcase_20 AC 585 ms
24,764 KB
testcase_21 AC 610 ms
25,132 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#!/usr/bin/env ruby

n, m = gets.split(' ').map &:to_i
S = gets.chomp
T = gets.chomp

# dp[i+1][j+1]: S i文字目までを変換してT[j]にするための手数と定義する
# S: pirikapirirara
# T: poporinapeperuto

dp = []

# Initialization
(0..n).each do |i|
  (0..m).each do |j|
    dp[i] ||= []
    dp[i][j] = j if i == 0
    dp[i][j] = i if j == 0
  end
end

(0...n).each do |i|
  (0...m).each do |j|
    c1 = S[i] == T[j] ? dp[i][j] : dp[i][j] + 1
    c2 = dp[i][j+1] + 1
    c3 = dp[i+1][j] + 1

    dp[i+1][j+1] = [c1,c2,c3].min
  end
end

puts dp[S.length][T.length]
0