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

22 lines
1.4 KiB
APL
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
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
}