Longest common substring: Difference between revisions

m
(Added XPL0 example.)
(10 intermediate revisions by 5 users not shown)
Line 214:
test
</pre>
 
=={{header|APL}}==
<syntaxhighlight lang="apl">lcs←{
sb←∪⊃,/{⌽¨,\⌽⍵}¨,\⍵
match←(sb(∨/⍷)¨⊂⍺)/sb
⊃((⌈/=⊢)≢¨match)/match
}</syntaxhighlight>
{{out}}
<syntaxhighlight lang="apl">
'testing123testing' lcs 'thisisatest'
test</syntaxhighlight>
 
=={{header|AppleScript}}==
Line 905 ⟶ 916:
 
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
 
sub Contains(s1: [uint8], s2: [uint8]): (r: uint8) is
r := 0;
while [s1] != 0 loop
var a := s1;
var b := s2;
while [b] != 0 and [a] == [b] loop
a := @next a;
b := @next b;
end loop;
if [b] == 0 then
r := 1;
return;
end if;
s1 := @next s1;
end loop;
end sub;
 
sub LCS(s1: [uint8], s2: [uint8], outbuf: [uint8]) is
if StrLen(s1) < StrLen(s2) then
var temp := s1;
s1 := s2;
s2 := temp;
end if;
 
var maxlen := StrLen(s2);
var length := maxlen;
while length > 0 loop
var start: intptr := 0;
while start + length <= maxlen loop
MemCopy(s2 + start, length, outbuf);
[outbuf + length + 1] := 0;
if Contains(s1, outbuf) != 0 then
return;
end if;
start := start + 1;
end loop;
length := length - 1;
end loop;
[outbuf] := 0;
end sub;
 
var lcs: uint8[64];
LCS("thisisatest", "testing123testing", &lcs[0]);
print(&lcs[0]);
print_nl();</syntaxhighlight>
{{out}}
<pre>test</pre>
=={{header|D}}==
{{trans|C#}}
Line 1,032 ⟶ 1,094:
comSubStr("thisisatest", "testing123testing") // "test"</syntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight>
func$ lcs a$ b$ .
if a$ = "" or b$ = ""
return ""
.
while b$ <> ""
for j = len b$ downto 1
l$ = substr b$ 1 j
for k = 1 to len a$ - j + 1
if substr a$ k j = l$
if len l$ > len max$
max$ = l$
.
break 2
.
.
.
b$ = substr b$ 2 9999
.
return max$
.
print lcs "thisisatest" "testing123testing"
print lcs "thisisatest" "stesting123testing"
print lcs "thisisatestxestinoo" "xxtesting123testing"
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 1,531 ⟶ 1,621:
=={{header|langur}}==
{{trans|Julia}}
<syntaxhighlight lang="langur">val .lcs = ffn(.s1, .s2) {
var .l, .r, .sublen = 1, 0, 0
for .i of .s1 {
Line 1,759 ⟶ 1,849:
S1: string = 'thisisatest' ;
 
S2: string = 'testing123isatestingtesting123testing' ;
 
 
Line 2,218 ⟶ 2,308:
main()
</syntaxhighlight>
<pre>test</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ 0 temp put
0 temp put
tuck dup size times
[ 2dup swap
0 temp put
0 swap witheach
[ unrot
over size
over = iff
[ drop
conclude ]
done
rot dip
[ 2dup peek ]
= tuck * +
dup temp take
max temp put ]
2drop
temp take
dup temp share > iff
[ temp release
i^ temp replace
temp put ]
else drop
behead drop ]
2drop
temp take dip
[ temp take split nip ]
split drop ] is lcs ( $ $ --> $ )
 
$ "thisisatest" $ "testing123testing" lcs echo$ cr</syntaxhighlight>
 
{{out}}
 
<pre>test</pre>
 
Line 2,293 ⟶ 2,421:
<pre>The longest common substring between 'thisisatest' and 'testing123testing' is 'test'.</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang"refal">$ENTRY Go {
= <Prout <LCS ('thisisatest') 'testing123testing'>>;
};
 
LCS {
(e.X) e.L e.X e.R = e.X;
() e.Y = ;
e.X e.Y, e.X: (s.L e.XL),
e.X: (e.XR s.R)
= <Longest (<LCS (e.XL) e.Y>) <LCS (e.XR) e.Y>>;
};
 
Longest {
(e.X) e.Y, <Lenw e.X>: s.LX e.X2,
<Lenw e.Y>: s.LY e.Y2,
<Compare s.LX s.LY>: '+' = e.X;
(e.X) e.Y = e.Y;
};</syntaxhighlight>
{{out}}
<pre>test</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program determines the LCSUBSTR (Longest Common Substring) via a function. */
Line 2,582 ⟶ 2,731:
"Some(Set(test))"
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program longest_common_substring;
print(lcs("thisisatest", "testing123testing"));
 
proc lcs(s1, s2);
if #s1 < #s2 then [s1, s2] := [s2, s1]; end if;
 
loop for l in [#s2, #s2-1..1] do
loop for s in [1..#s2-l+1] do
if (substr := s2(s..s+l)) in s1 then
return substr;
end if;
end loop;
end loop;
 
return "";
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>test</pre>
 
=={{header|Sidef}}==
Line 2,719 ⟶ 2,889:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var lcs = Fn.new { |a, b|
var la = a.count
var lb = b.count
990

edits