in reply to LCS efficiency problem

Pardon my abruptness in advance. There's a lot of ground to cover.

Those are bugs to fix, not performance enhancements (except the tip to make everything lowercase). I didn't even try running the program,

Update: Changed @s[$start .. $end-$start+1] to @S[$start..$end]

Update: Added off-by-one error.

Replies are listed 'Best First'.
Re^2: LCS efficiency problem
by BrowserUk (Patriarch) on Jun 05, 2008 at 06:44 UTC

      split / / isn't magical. You're thinking of split ' '.

      >perl -le"@a = split / /, 'abc def'; print 0+@a" 3 >perl -le"@a = split ' ', 'abc def'; print 0+@a" 2

      split ' ' is slightly better than split /\s+/, but like I said, I wouldn't use it anyway.

Re^2: LCS efficiency problem
by lima1 (Curate) on Jun 06, 2008 at 07:13 UTC
    Starting with 1 is part of the dynamic programming algorithm, no bug.
    my @S = (undef, map { lc } split(/\w+/, $s));
    should fix the wordlists (so that eq is enough). Further filtering as suggested below is a good idea, do that!

      Oops, missed the undef.

      But note that starting at one has nothing to do with the dynamic programming technique. If you wanted to start at zero (say if @S and @T are inputs to the function), then just replace

      $L[$i-1][$j-1] ||= 0; $L[$i][$j] = $L[$i-1][$j-1] + 1;
      with
      if ($i && $j) { $L[$i-1][$j-1] ||= 0; $L[$i][$j] = $L[$i-1][$j-1] + 1; } else { $L[$i][$j] = 1; }