class Yukicoder def initialize n, m = gets.chomp.split.map(&:to_i) s = gets.chomp t = gets.chomp dp = Array.new(n+1){ Array.new(m+1, [n,m].max)} dp[0][0] = 0 1.upto(n) do |i| 1.upto(m) do |j| 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].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 end p dp[n][m] end end Yukicoder.new