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 s.chars.each.with_index(1) do |c1, i| t.chars.each.with_index(1) do |c2, j| if c1 == c2 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