Skip to content
 

Perl fun (at last)

After reading something about dynamic programming problems (and thanks to Perl's very liberal parser for the alignment)...

Copy and distribution of the code published in this page, with or without
modification, are permitted in any medium without royalty provided the copyright
notice (see bottom of page) and this notice are preserved.
@v=map{-2+ord}"a\x0c>oq\"e0#zo#iBtd%agx#cfR"=~/./g;($W,$w,$n)=(606,607,23);@w=
map{ord}"a#Q, J!!A%(V&)#(M-!!P).P"=~/./g;for($w..14567){if($_%$w){$p=$_-$w;$m[
$_]=$w[$a=int($_/$w)]>($_%$w)?$m[$p]:($x=$m[$p])>=($y=$m[$p-$w[$a]]+$v[$a])?$x
:$y}}for(;$n;$n--){$m[$a=$n*$w+$W]!=$m[$a-$w]and print chr$v[$n]and$W-=$w[$n]}

The only fact it exploits is that undefined values implicitly become 0 when used in calculations. Indeed, if the necessary declarations and initializations are added (not shown), the code runs without warnings under use warnings; and use strict;.

Be Sociable, Share!

Leave a Reply

(required)