class Yukicoder def initialize n, m = gets.chomp.split.map(&:to_i) s = gets.chomp t = gets.chomp dp = Array.new(n+2){ Array.new(m+2, [n,m].max)} dp[0] = [*(0..m)] 1.upto(n) do |i| 1.upto(m) do |j| if s[i] == t[j] dp[i][j] = [dp[i][j], dp[i-1][j-1]].min end dp[i][j] = [dp[i][j], dp[i-1][j]+1].min dp[i][j] = [dp[i][j], dp[i][j-1]+1].min dp[i][j] = [dp[i][j], dp[i-1][j-1]+1].min end end p dp[n][m] end end Yukicoder.new