sub seq{ my @x=split"",pop; my $s=pop; my $cc=0; for my $k (0..$#x-1) { $c = seq1($cc, $s, @x[$k..$#x]); $cc = ($c, $cc)[ $c < $cc ] } $cc } sub seq1 { my($cc, $s, @x) = @_; my $c=pos($s)=0; my $op=0; my $cy=0; for my $k (0..$#x) { $s =~ /$x[$k]/gc or next; if ($cc <= @x - $k) { # what if we skip this char? my $cx = $c + seq1( $cy, substr($s, $op), @x[$k+1 .. $#x]); $cy = ($cx, $cy)[$cx < $cy]; } $op = pos($s); $c++; } $c = ($cy, $c)[ $cy < $c ]; }