diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-11-17 15:55:57 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-11-17 15:55:57 -0600 |
commit | 56e59b92a9170f87083f3a6f6c9ba02092cbfbc1 (patch) | |
tree | c089be1b7b40d18d3d8109b62844d5fe18538307 | |
parent | de60194c4c0e7a8b509c42f4b09f9795e30a5c7f (diff) | |
download | rosalind-56e59b92a9170f87083f3a6f6c9ba02092cbfbc1.tar.gz rosalind-56e59b92a9170f87083f3a6f6c9ba02092cbfbc1.zip |
another solution
-rw-r--r-- | LCSQ.pl | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -0,0 +1,29 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.016; + +no warnings 'recursion'; + +use Memoize; + +chomp(my $str1 = <>); +chomp(my $str2 = <>); + +say lcsq($str1, $str2); + +sub lcsq { + my ($str1, $str2) = @_; + + return '' unless length($str1) && length($str2); + if (substr($str1, 0, 1) eq substr($str2, 0, 1)) { + return substr($str1, 0, 1) + . lcsq(substr($str1, 1), substr($str2, 1)); + } + else { + my $lcsq1 = lcsq(substr($str1, 1), $str2); + my $lcsq2 = lcsq($str1, substr($str2, 1)); + return length($lcsq1) > length($lcsq2) ? $lcsq1 : $lcsq2; + } +} +BEGIN { memoize('lcsq') }; |