Longest common subsequence: Difference between revisions

Content added Content deleted
(add m4)
(add caching to m4, mostly to make performance more bearable)
Line 405: Line 405:
=={{header|M4}}==
=={{header|M4}}==
<lang M4>
<lang M4>
define(`set2d',`define(`$1[$2][$3]',`$4')')
define(`get2d',`defn($1[$2][$3])')
define(`tryboth',
define(`tryboth',
`pushdef(`x',lcs(`$1',substr(`$2',1),`$1 $2'))`'pushdef(`y',
`pushdef(`x',lcs(`$1',substr(`$2',1),`$1 $2'))`'pushdef(`y',
Line 414: Line 416:
`tryboth(`$1',`$2')')')
`tryboth(`$1',`$2')')')
define(`lcs',
define(`lcs',
`ifelse(`$1',`',`',`$2',`',`',`checkfirst(`$1',`$2')')')
`ifelse(get2d(`c',`$1',`$2'),`',
`pushdef(`a',ifelse(
`$1',`',`',
`$2',`',`',
`checkfirst(`$1',`$2')'))`'a`'set2d(`c',`$1',`$2',a)`'popdef(`a')',
`get2d(`c',`$1',`$2')')')

lcs(`1234',`1224533324')

lcs(`thisisatest',`testing123testing')
</lang>
</lang>
Note: the caching (set2d/get2d) obscures the code even more than usual, but is necessary in order to get the second test to run in a reasonable amount of time.


=={{header|Mathematica}}==
=={{header|Mathematica}}==