Shortest common supersequence: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 18:
{{trans|C++}}
 
<langsyntaxhighlight lang="11l">F scs(String x, y)
I x.empty
R y
Line 30:
R x[0]‘’scs(x[1..], y)
 
print(scs(‘abcbdab’, ‘bdcaba’))</langsyntaxhighlight>
 
{{out}}
Line 39:
=={{header|Ada}}==
{{trans|C++}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Shortest is
Line 75:
Exercise ("abcbdab", "bdcaba");
Exercise ("WEASELS", "WARDANCE");
end Shortest;</langsyntaxhighlight>
{{out}}
<pre>
Line 84:
=={{header|ALGOL 68}}==
{{Trans|C++}}
<langsyntaxhighlight lang="algol68">BEGIN
PRIO SCS = 1;
# returns the shortest common supersequence of x and y #
Line 102:
print( ( "abcbdab" SCS "bdcaba", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 110:
=={{header|C}}==
The C99 code here isn't all that different from Levenstein distance calculation.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 163:
printf("SCS(%s, %s) -> %s\n", x, y, res);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 171:
=={{header|C sharp}}==
{{trans|Java}}This is based on the Java version, but with added caching.
<langsyntaxhighlight lang="csharp">public class ShortestCommonSupersequence
{
Dictionary<(string, string), string> cache = new();
Line 205:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>abdcabdab</pre>
Line 211:
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
std::string scs(std::string x, std::string y) {
Line 234:
std::cout << res << '\n';
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>abdcabdab</pre>
Line 240:
=={{header|D}}==
{{trans|Racket}}
<langsyntaxhighlight lang="d">import std.stdio, std.functional, std.array, std.range;
 
dstring scs(in dstring x, in dstring y) nothrow @safe {
Line 256:
void main() @safe {
scs("abcbdab", "bdcaba").writeln;
}</langsyntaxhighlight>
{{out}}
<pre>abdcabdab</pre>
Line 264:
{{works with|Elixir|1.3}}
uses 'LCS' from [[Longest common subsequence#Elixir|here]]
<langsyntaxhighlight lang="elixir">defmodule SCS do
def scs(u, v) do
lcs = LCS.lcs(u, v) |> to_charlist
Line 278:
u = "abcbdab"
v = "bdcaba"
IO.puts "SCS(#{u}, #{v}) = #{SCS.scs(u, v)}"</langsyntaxhighlight>
 
{{out}}
Line 287:
=={{header|Factor}}==
{{trans|Julia}}
<langsyntaxhighlight lang="factor">USING: combinators io kernel locals math memoize sequences ;
 
MEMO:: scs ( x y -- seq )
Line 300:
} cond ;
 
"abcbdab" "bdcaba" scs print</langsyntaxhighlight>
{{out}}
<pre>
Line 308:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 363:
v := "bdcaba"
fmt.Println(scs(u, v))
}</langsyntaxhighlight>
 
{{out}}
Line 371:
=={{header|Haskell}}==
{{trans|C++}}
<langsyntaxhighlight Haskelllang="haskell">scs :: Eq a => [a] -> [a] -> [a]
scs [] ys = ys
scs xs [] = xs
Line 383:
| otherwise = y : vs
 
main = putStrLn $ scs "abcbdab" "bdcaba"</langsyntaxhighlight>
{{out}}
<pre>abdcabdab</pre>
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">public class ShortestCommonSuperSequence {
private static boolean isEmpty(String s) {
return null == s || s.isEmpty();
Line 415:
System.out.println(scs("abcbdab", "bdcaba"));
}
}</langsyntaxhighlight>
{{out}}
<pre>abdcabdab</pre>
Line 423:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># largest common substring
# Uses recursion, taking advantage of jq's TCO
def lcs:
Line 459:
| .sb ;
[ "abcbdab", "bdcaba" ] | scs</langsyntaxhighlight>
{{out}}
<pre>
Line 467:
=={{header|Julia}}==
{{trans|D}}
<langsyntaxhighlight Julialang="julia">using Memoize
 
@memoize function scs(x, y)
Line 484:
 
println(scs("abcbdab", "bdcaba"))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 492:
=={{header|Kotlin}}==
Uses 'lcs' function from [[Longest common subsequence#Kotlin]]:
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun lcs(x: String, y: String): String {
Line 524:
val v = "bdcaba"
println(scs(u, v))
}</langsyntaxhighlight>
 
{{out}}
Line 532:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[RosettaShortestCommonSuperSequence]
RosettaShortestCommonSuperSequence[aa_String, bb_String] :=
Module[{lcs, scs, a = aa, b = bb},
Line 556:
]
RosettaShortestCommonSuperSequence["abcbdab", "bdcaba"]
RosettaShortestCommonSuperSequence["WEASELS", "WARDANCE"]</langsyntaxhighlight>
{{out}}
<pre>bdcabcbdaba
Line 563:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">proc lcs(x, y: string): string =
if x.len == 0 or y.len == 0: return
let x1 = x[0..^2]
Line 591:
let u = "abcbdab"
let v = "bdcaba"
echo scs(u, v)</langsyntaxhighlight>
 
{{out}}
Line 597:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub lcs { # longest common subsequence
my( $u, $v ) = @_;
return '' unless length($u) and length($v);
Line 628:
printf "Longest common subsequence: %s\n", lcs $u, $v;
printf "Shortest common supersquence: %s\n", scs $u, $v;
</syntaxhighlight>
</lang>
{{out}}
<pre>Strings abcbdab bdcaba
Line 637:
=={{header|Phix}}==
{{trans|Python}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">longest_common_subsequence</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
Line 679:
<span style="color: #0000FF;">?</span><span style="color: #000000;">shortest_common_supersequence</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abcbdab"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"bdcaba"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">shortest_common_supersequence</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"WEASELS"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"WARDANCE"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 687:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
# Use the Longest Common Subsequence algorithm
 
Line 709:
# append remaining characters
return scs + a + b
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 719:
=={{header|Racket}}==
{{trans|C}}This program is based on the C implementation, but use memorization instead of dynamic programming. More explanations about the memorization part in http://blog.racket-lang.org/2012/08/dynamic-programming-versus-memoization.html .
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(struct link (len letters))
Line 751:
(list->string (link-letters (scs/list (string->list x) (string->list y)))))
 
(scs "abcbdab" "bdcaba")</langsyntaxhighlight>
{{out}}
<pre>"abdcabdab"</pre>
Line 759:
 
Using 'lcs' routine from [[Longest_common_subsequence#Raku|Longest common subsequence task]]
<syntaxhighlight lang="raku" perl6line>sub lcs(Str $xstr, Str $ystr) { # longest common subsequence
return "" unless $xstr && $ystr;
my ($x, $xs, $y, $ys) = $xstr.substr(0, 1), $xstr.substr(1), $ystr.substr(0, 1), $ystr.substr(1);
Line 782:
printf "Strings: %s %s\n", $u, $v;
printf "Longest common subsequence: %s\n", lcs $u, $v;
printf "Shortest common supersquence: %s\n", scs $u, $v;</langsyntaxhighlight>
{{out}}
<pre>Strings: abcbdab bdcaba
Line 790:
=={{header|REXX}}==
{{trans|RING}}
<langsyntaxhighlight lang="rexx">/*REXX program finds the Shortest common supersequence (SCS) of two character strings.*/
parse arg u v . /*obtain optional arguments from the CL*/
if u=='' | u=="," then u= 'abcbdab' /*Not specified? Then use the default.*/
Line 805:
end /*n*/
say
say 'shortest common supersequence=' $ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 829:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Shortest common supersequence
 
Line 851:
next
see svect
</syntaxhighlight>
</lang>
Output:
<pre>
Line 860:
{{trans|Tcl}}
uses 'lcs' from [[Longest common subsequence#Ruby|here]]
<langsyntaxhighlight lang="ruby">require 'lcs'
 
def scs(u, v)
Line 887:
u = "abcbdab"
v = "bdcaba"
puts "SCS(#{u}, #{v}) = #{scs(u, v)}"</langsyntaxhighlight>
 
{{out}}
Line 897:
{{trans|Perl}}
Uses the ''lcs'' function defined [http://rosettacode.org/wiki/Longest_common_subsequence#Sidef here].
<langsyntaxhighlight lang="ruby">func scs(u, v) {
var ls = lcs(u, v).chars
var pat = Regex('(.*)'+ls.join('(.*)')+'(.*)')
Line 907:
}
 
say scs("abcbdab", "bdcaba")</langsyntaxhighlight>
{{out}}
<pre>
Line 915:
=={{header|Tcl}}==
This example uses either of the <code>lcs</code> implementations from [[longest common subsequence#Tcl|here]], assumed renamed to <tt>lcs</tt>…
<langsyntaxhighlight lang="tcl">proc scs {u v} {
set lcs [lcs $u $v]
set scs ""
Line 945:
append scs [string range $u $ui end] [string range $v $vi end]
return $scs
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">set u "abcbdab"
set v "bdcaba"
puts "SCS($u,$v) = [scs $u $v]"</langsyntaxhighlight>
{{out}}
<pre>SCS(abcbdab,bdcaba) = abdcabdab</pre>
Line 955:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">var lcs // recursive
lcs = Fn.new { |x, y|
if (x.count == 0 || y.count == 0) return ""
Line 991:
var u = "abcbdab"
var v = "bdcaba"
System.print(scs.call(u, v))</langsyntaxhighlight>
 
{{out}}
Line 1,000:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">class Link{ var len,letter,next;
fcn init(l=0,c="",lnk=Void){ len,letter,next=l,c,lnk; }
}
Line 1,027:
lp:=lnk[0][0]; while(lp){ out.write(lp.letter); lp=lp.next; }
out.close()
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">scs("abcbdab","bdcaba", Sink(String)).println();</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits