RosettaCodeData/Task/Longest-common-subsequence/APL/longest-common-subsequence.apl

22 lines
1.4 KiB
APL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

lcs{
⎕IO0
betterof{(</+/¨ ) } ⍝ better of 2 selections
cmbn{,.,/(),} ⍝ combine lists
rr{/>/1 ¯1[1]¨} ⍝ rising rows
hmrr{/(rr )/=\} ⍝ has monotonically rising rows
rnbc{{/}¨[0]×} ⍝ row numbers by column
validhmrrcmbnrnbc ⍝ any valid solutions?
a w(</¨ ) ⍝ longest first
matchesa.=w
aps{[;+]}{(/2)2*} ⍝ all possible subsequences
swps{/~(~)} ⍝ subsequences with possible solns
ssttmatches swps apsw ⍝ subsequences to try
w/{
0 ⍝ initial selection
(+/)+/[;0]: ⍝ no scope to improve
this betterof{×valid /matches}[;0] ⍝ try to improve
1=1:this ⍝ nothing left to try
this 1[1] ⍝ keep looking
}sstt
}