langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,10 @@
|
|||
let longest xs ys = if List.length xs > List.length ys then xs else ys
|
||||
|
||||
let rec lcs a b = match a, b with
|
||||
[], _
|
||||
| _, [] -> []
|
||||
| x::xs, y::ys ->
|
||||
if x = y then
|
||||
x :: lcs xs ys
|
||||
else
|
||||
longest (lcs a ys) (lcs xs b)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
let lcs xs' ys' =
|
||||
let xs = Array.of_list xs'
|
||||
and ys = Array.of_list ys' in
|
||||
let n = Array.length xs
|
||||
and m = Array.length ys in
|
||||
let a = Array.make_matrix (n+1) (m+1) [] in
|
||||
for i = n-1 downto 0 do
|
||||
for j = m-1 downto 0 do
|
||||
a.(i).(j) <- if xs.(i) = ys.(j) then
|
||||
xs.(i) :: a.(i+1).(j+1)
|
||||
else
|
||||
longest a.(i).(j+1) a.(i+1).(j)
|
||||
done
|
||||
done;
|
||||
a.(0).(0)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let list_of_string str =
|
||||
let result = ref [] in
|
||||
String.iter (fun x -> result := x :: !result)
|
||||
str;
|
||||
List.rev !result
|
||||
|
||||
let string_of_list lst =
|
||||
let result = String.create (List.length lst) in
|
||||
ignore (List.fold_left (fun i x -> result.[i] <- x; i+1) 0 lst);
|
||||
result
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
declare
|
||||
fun {LCS Xs Ys}
|
||||
case [Xs Ys]
|
||||
of [nil _] then nil
|
||||
[] [_ nil] then nil
|
||||
[] [X|Xr Y|Yr] andthen X==Y then X|{LCS Xr Yr}
|
||||
[] [_|Xr _|Yr] then {Longest {LCS Xs Yr} {LCS Xr Ys}}
|
||||
end
|
||||
end
|
||||
|
||||
fun {Longest Xs Ys}
|
||||
if {Length Xs} > {Length Ys} then Xs else Ys end
|
||||
end
|
||||
in
|
||||
{System.showInfo {LCS "thisisatest" "testing123testing"}}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
Program LongestCommonSubsequence(output);
|
||||
|
||||
function lcs(a, b: string): string;
|
||||
var
|
||||
x, y: string;
|
||||
lenga, lengb: integer;
|
||||
begin
|
||||
lenga := length(a);
|
||||
lengb := length(b);
|
||||
lcs := '';
|
||||
if (lenga > 0) and (lengb > 0) then
|
||||
if a[lenga] = b[lengb] then
|
||||
lcs := lcs(copy(a, 1, lenga-1), copy(b, 1, lengb-1)) + a[lenga]
|
||||
else
|
||||
begin
|
||||
x := lcs(a, copy(b, 1, lengb-1));
|
||||
y := lcs(copy(a, 1, lenga-1), b);
|
||||
if length(x) > length(y) then
|
||||
lcs := x
|
||||
else
|
||||
lcs := y;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
s1, s2: string;
|
||||
begin
|
||||
s1 := 'thisisatest';
|
||||
s2 := 'testing123testing';
|
||||
writeln (lcs(s1, s2));
|
||||
s1 := '1234';
|
||||
s2 := '1224533324';
|
||||
writeln (lcs(s1, s2));
|
||||
end.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
sub lcs(Str $xstr, Str $ystr) {
|
||||
return "" unless $xstr & $ystr;
|
||||
|
||||
my ($x, $xs, $y, $ys) = $xstr.substr(0, 1), $xstr.substr(1), $ystr.substr(0, 1), $ystr.substr(1);
|
||||
return $x eq $y
|
||||
?? $x ~ lcs($xs, $ys)
|
||||
!! max({ $^a.chars }, lcs($xstr, $ys), lcs($xs, $ystr) );
|
||||
}
|
||||
|
||||
say lcs("thisisatest", "testing123testing");
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
sub lcs(Str $xstr, Str $ystr) {
|
||||
my ($xlen, $ylen) = ($xstr, $ystr)>>.chars;
|
||||
my @lengths = map {[(0) xx ($ylen+1)]}, 0..$xlen;
|
||||
|
||||
for $xstr.comb.kv -> $i, $x {
|
||||
for $ystr.comb.kv -> $j, $y {
|
||||
@lengths[$i+1][$j+1] = $x eq $y ?? @lengths[$i][$j]+1 !! (@lengths[$i+1][$j], @lengths[$i][$j+1]).max;
|
||||
}
|
||||
}
|
||||
|
||||
my @x = $xstr.comb;
|
||||
my ($x, $y) = ($xlen, $ylen);
|
||||
my $result = "";
|
||||
while $x != 0 && $y != 0 {
|
||||
if @lengths[$x][$y] == @lengths[$x-1][$y] {
|
||||
$x--;
|
||||
}
|
||||
elsif @lengths[$x][$y] == @lengths[$x][$y-1] {
|
||||
$y--;
|
||||
}
|
||||
else {
|
||||
$result = @x[$x-1] ~ $result;
|
||||
$x--;
|
||||
$y--;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
say lcs("thisisatest", "testing123testing");
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
Procedure.s lcs(a$, b$)
|
||||
Protected x$ , lcs$
|
||||
If Len(a$) = 0 Or Len(b$) = 0
|
||||
lcs$ = ""
|
||||
ElseIf Right(a$, 1) = Right(b$, 1)
|
||||
lcs$ = lcs(Left(a$, Len(a$) - 1), Left(b$, Len(b$) - 1)) + Right(a$, 1)
|
||||
Else
|
||||
x$ = lcs(a$, Left(b$, Len(b$) - 1))
|
||||
y$ = lcs(Left(a$, Len(a$) - 1), b$)
|
||||
If Len(x$) > Len(y$)
|
||||
lcs$ = x$
|
||||
Else
|
||||
lcs$ = y$
|
||||
EndIf
|
||||
EndIf
|
||||
ProcedureReturn lcs$
|
||||
EndProcedure
|
||||
OpenConsole()
|
||||
PrintN( lcs("thisisatest", "testing123testing"))
|
||||
PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
a$ = "aebdaef"
|
||||
b$ = "cacbac"
|
||||
print lcs$(a$,b$)
|
||||
end
|
||||
|
||||
FUNCTION lcs$(a$, b$)
|
||||
IF a$ = "" OR b$ = "" THEN
|
||||
lcs$ = ""
|
||||
goto [ext]
|
||||
end if
|
||||
|
||||
IF RIGHT$(a$, 1) = RIGHT$(b$, 1) THEN
|
||||
lcs$ = lcs$(LEFT$(a$, LEN(a$) - 1), LEFT$(b$, LEN(b$) - 1)) + RIGHT$(a$, 1)
|
||||
goto [ext]
|
||||
ELSE
|
||||
x1$ = lcs$(a$, LEFT$(b$, LEN(b$) - 1))
|
||||
x2$ = lcs$(LEFT$(a$, LEN(a$) - 1), b$)
|
||||
IF LEN(x1$) > LEN(x2$) THEN
|
||||
lcs$ = x1$
|
||||
goto [ext]
|
||||
ELSE
|
||||
lcs$ = x2$
|
||||
goto [ext]
|
||||
END IF
|
||||
END IF
|
||||
[ext]
|
||||
END FUNCTION
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
op .longest(a, b);
|
||||
return if #a > #b then a else b end;
|
||||
end .longest;
|
||||
|
||||
procedure lcs(a, b);
|
||||
if exists empty in {a, b} | #empty = 0 then
|
||||
return empty;
|
||||
elseif a(1) = b(1) then
|
||||
return a(1) + lcs(a(2..), b(2..));
|
||||
else
|
||||
return lcs(a(2..), b) .longest lcs(a, b(2..));
|
||||
end;
|
||||
end lcs;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
s1@(Sequence traits) longestCommonSubsequenceWith: s2@(Sequence traits)
|
||||
[
|
||||
s1 isEmpty \/ s2 isEmpty ifTrue: [^ {}].
|
||||
s1 last = s2 last
|
||||
ifTrue: [(s1 allButLast longestCommonSubsequenceWith: s2 allButLast) copyWith: s1 last]
|
||||
ifFalse: [| x y |
|
||||
x: (s1 longestCommonSubsequenceWith: s2 allButLast).
|
||||
y: (s1 allButLast longestCommonSubsequenceWith: s2).
|
||||
x length > y length ifTrue: [x] ifFalse: [y]]
|
||||
].
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
s1@(Sequence traits) longestCommonSubsequenceWith: s2@(Sequence traits)
|
||||
[| lengths |
|
||||
lengths: (ArrayMD newWithDimensions: {s1 length `cache. s2 length `cache} defaultElement: 0).
|
||||
s1 doWithIndex: [| :elem1 :index1 |
|
||||
s2 doWithIndex: [| :elem2 :index2 |
|
||||
elem1 = elem2
|
||||
ifTrue: [lengths at: {index1 + 1. index2 + 1} put: (lengths at: {index1. index2}) + 1]
|
||||
ifFalse: [lengths at: {index1 + 1. index2 + 1} put:
|
||||
((lengths at: {index1 + 1. index2}) max: (lengths at: {index1. index2 + 1}))]]].
|
||||
([| :result index1 index2 |
|
||||
index1: s1 length.
|
||||
index2: s2 length.
|
||||
[index1 isPositive /\ index2 isPositive]
|
||||
whileTrue:
|
||||
[(lengths at: {index1. index2}) = (lengths at: {index1 - 1. index2})
|
||||
ifTrue: [index1: index1 - 1]
|
||||
ifFalse: [(lengths at: {index1. index2}) = (lengths at: {index1. index2 - 1})]
|
||||
ifTrue: [index2: index2 - 1]
|
||||
ifFalse: ["assert: (s1 at: index1 - 1) = (s2 at: index2 - 1)."
|
||||
result nextPut: (s1 at: index1 - 1).
|
||||
index1: index1 - 1.
|
||||
index2: index2 - 1]]
|
||||
] writingAs: s1) reverse
|
||||
].
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#import std
|
||||
|
||||
lcs = ~&alrB^& ~&E?abh/~&alh2fabt2RC @faltPrXlrtPXXPW leql?/~&r ~&l
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#cast %s
|
||||
|
||||
example = lcs('thisisatest','testing123testing')
|
||||
Loading…
Add table
Add a link
Reference in a new issue