Longest common substring: Difference between revisions

m
 
(7 intermediate revisions by 4 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,035 ⟶ 1,097:
=={{header|EasyLang}}==
 
<syntaxhighlight lang=easylang>
func$ lcs a$ b$ .
if a$ = "" or b$ = ""
Line 1,052 ⟶ 1,114:
.
.
b$ = substr b$ 2 -19999
.
return max$
Line 1,559 ⟶ 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,787 ⟶ 1,849:
S1: string = 'thisisatest' ;
 
S2: string = 'testing123isatestingtesting123testing' ;
 
 
Line 2,359 ⟶ 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,648 ⟶ 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,785 ⟶ 2,889:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var lcs = Fn.new { |a, b|
var la = a.count
var lb = b.count
885

edits