use strict; use warnings; my($len1, $len2) = split ' ', ; my @ar1 = split('', ); my @ar2 = split('', ); my @dist; for(0..$len1){ $dist[$_][0] = $_; } for(0..$len2){ $dist[0][$_] = $_; } my $rep; my $ref; my @costs; for my $i (1..$len1){ for my $j (1..$len2){ $rep = $ar1[$i-1] eq $ar2[$j-1] ? 0 : 1; @costs = ($dist[$i-1][$j] + 1, $dist[$i][$j-1] + 1, $dist[$i-1][$j-1] + $rep); $dist[$i][$j] = (sort {$a <=> $b} @costs)[0]; } } print $dist[$len1][$len2];