Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -1,51 +0,0 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Longest_Increasing_Subsequence is
|
||||
type Sequence is array (Positive range <>) of Integer;
|
||||
|
||||
Seq1 : constant Sequence :=
|
||||
(3, 2, 6, 4, 5, 1);
|
||||
Seq2 : constant Sequence :=
|
||||
(0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15);
|
||||
|
||||
function Lis (Seq : Sequence) return Sequence is
|
||||
Best : Sequence (Seq'Range) := (others => 1);
|
||||
Pred : Sequence (Seq'Range) := (others => 0);
|
||||
Best_Idx, Max_Len : Natural := 0;
|
||||
begin
|
||||
-- Calculate LIS length for every end point
|
||||
for I in Seq'Range loop
|
||||
for J in 1 .. I - 1 loop
|
||||
if Seq (J) < Seq (I) and then
|
||||
Best (I) < 1 + Best (J) then
|
||||
Best (I) := 1 + Best (J);
|
||||
Pred (I) := J;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
-- Find tail of global LIS
|
||||
for I in Best'Range loop
|
||||
if Best (I) > Max_Len then
|
||||
Best_Idx := I;
|
||||
Max_Len := Best (I);
|
||||
end if;
|
||||
end loop;
|
||||
|
||||
-- Trace sequence
|
||||
declare
|
||||
Lis : Sequence (1 .. Max_Len);
|
||||
begin
|
||||
for I in reverse 1 .. Max_Len loop
|
||||
Lis (I) := Seq (Best_Idx);
|
||||
Best_Idx := Pred (Best_Idx);
|
||||
end loop;
|
||||
|
||||
return Lis;
|
||||
end;
|
||||
end Lis;
|
||||
|
||||
begin
|
||||
Ada.Text_IO.Put_Line (Lis (Seq1)'Image);
|
||||
Ada.Text_IO.Put_Line (Lis (Seq2)'Image);
|
||||
end Longest_Increasing_Subsequence;
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
lis: function [d][
|
||||
l: new [[]]
|
||||
l: [[]]
|
||||
loop d 'num [
|
||||
x: []
|
||||
loop l 'seq [
|
||||
|
|
|
|||
|
|
@ -1,19 +1,19 @@
|
|||
function lis(arr::Vector)
|
||||
if length(arr) == 0 return copy(arr) end
|
||||
L = Vector{typeof(arr)}(length(arr))
|
||||
L[1] = [arr[1]]
|
||||
function lis(arr::Vector{T}) where T
|
||||
isempty(arr) && return copy(arr)
|
||||
allsequences = Vector{Vector{T}}(undef, length(arr))
|
||||
allsequences[begin] = [arr[begin]]
|
||||
|
||||
for i in 2:length(arr)
|
||||
nextL = []
|
||||
for j in 1:i
|
||||
if arr[j] < arr[i] && length(L[j]) ≥ length(nextL)
|
||||
nextL = L[j]
|
||||
for i in firstindex(arr)+1:lastindex(arr)
|
||||
nextseq = T[]
|
||||
for j in firstindex(arr):i
|
||||
if arr[j] < arr[i] && length(allsequences[j]) ≥ length(nextseq)
|
||||
nextseq = allsequences[j]
|
||||
end
|
||||
end
|
||||
L[i] = vcat(nextL, arr[i])
|
||||
allsequences[i] = push!(nextseq, arr[i])
|
||||
end
|
||||
|
||||
return L[indmax(length.(L))]
|
||||
_, idx = findmax(length, allsequences)
|
||||
return allsequences[idx]
|
||||
end
|
||||
|
||||
@show lis([3, 2, 6, 4, 5, 1])
|
||||
|
|
|
|||
|
|
@ -1,40 +1,38 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">lis</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">len</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">lo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">len</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">hi</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">mid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">ceil</span><span style="color: #0000FF;">((</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">mid</span><span style="color: #0000FF;">]]<</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">lo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mid</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mid</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">lo</span><span style="color: #0000FF;">></span><span style="color: #000000;">len</span> <span style="color: #008080;">then</span> <span style="color: #000000;">len</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lo</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">len</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">len</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
with javascript_semantics
|
||||
function lis(sequence x, integer n = length(x))
|
||||
sequence p = repeat(0,n),
|
||||
m = repeat(0,n)
|
||||
integer len = 0
|
||||
for i=1 to n do
|
||||
integer lo = 1
|
||||
integer hi = len
|
||||
while lo<=hi do
|
||||
integer mid = ceil((lo+hi)/2)
|
||||
if x[m[mid]]<x[i] then
|
||||
lo = mid + 1
|
||||
else
|
||||
hi = mid - 1
|
||||
end if
|
||||
end while
|
||||
if lo>1 then
|
||||
p[i] = m[lo-1]
|
||||
end if
|
||||
m[lo] = i
|
||||
if lo>len then len = lo end if
|
||||
end for
|
||||
sequence res = repeat(0,len)
|
||||
if len>0 then
|
||||
integer k = m[len]
|
||||
for i=len to 1 by -1 do
|
||||
res[i] = x[k]
|
||||
k = p[k]
|
||||
end for
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15</span><span style="color: #0000FF;">}}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">lis</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
constant tests = {{3, 2, 6, 4, 5, 1},
|
||||
{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}}
|
||||
for i=1 to length(tests) do
|
||||
?lis(tests[i])
|
||||
end for
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
function Get-LongestSubsequence ( [int[]]$A )
|
||||
{
|
||||
If ( $A.Count -lt 2 ) { return $A }
|
||||
|
||||
# Start with an "empty" pile
|
||||
# (We will only store the top value in each "pile".)
|
||||
$Pile = @( [int]::MaxValue )
|
||||
$Last = 0
|
||||
|
||||
# Hashtable to hold the back pointers
|
||||
$BP = @{}
|
||||
|
||||
# For each number in the orginal sequence...
|
||||
ForEach ( $N in $A )
|
||||
{
|
||||
# Find the first pile with a value greater than N
|
||||
$i = 0..$Last | Where { $N -lt $Pile[$_] } | Select -First 1
|
||||
|
||||
# Place N on the pile
|
||||
$Pile[$i] = $N
|
||||
|
||||
# Set the back pointer for this value to the value of the previous pile
|
||||
$BP["$N"] = $Pile[$i-1]
|
||||
|
||||
# If this is the previously empty pile, add a new empty pile
|
||||
If ( $i -eq $Last )
|
||||
{
|
||||
$Pile += @( [int]::MaxValue )
|
||||
$Last++
|
||||
}
|
||||
}
|
||||
|
||||
# Ignore the empty pile
|
||||
$Last--
|
||||
|
||||
# Start with the value of the last pile
|
||||
$N = $Pile[$Last]
|
||||
$S = @( $N )
|
||||
|
||||
# Add the remainder of the values by walking through the back pointers
|
||||
ForEach ( $i in $Last..1 )
|
||||
{
|
||||
$S += ( $N = $BP["$N"] )
|
||||
}
|
||||
|
||||
# Return the series (reversed into the correct order)
|
||||
return $S[$Last..0]
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
( Get-LongestSubsequence 3, 2, 6, 4, 5, 1 ) -join ', '
|
||||
( Get-LongestSubsequence 0, 8, 4, 12, 2, 10, 6, 16, 14, 1, 9, 5, 13, 3, 11, 7, 15 ) -join ', '
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
Function LIS(arr)
|
||||
n = UBound(arr)
|
||||
Dim p()
|
||||
ReDim p(n)
|
||||
Dim m()
|
||||
ReDim m(n)
|
||||
l = 0
|
||||
For i = 0 To n
|
||||
lo = 1
|
||||
hi = l
|
||||
Do While lo <= hi
|
||||
middle = Int((lo+hi)/2)
|
||||
If arr(m(middle)) < arr(i) Then
|
||||
lo = middle + 1
|
||||
Else
|
||||
hi = middle - 1
|
||||
End If
|
||||
Loop
|
||||
newl = lo
|
||||
p(i) = m(newl-1)
|
||||
m(newl) = i
|
||||
If newL > l Then
|
||||
l = newl
|
||||
End If
|
||||
Next
|
||||
Dim s()
|
||||
ReDim s(l)
|
||||
k = m(l)
|
||||
For i = l-1 To 0 Step - 1
|
||||
s(i) = arr(k)
|
||||
k = p(k)
|
||||
Next
|
||||
LIS = Join(s,",")
|
||||
End Function
|
||||
|
||||
WScript.StdOut.WriteLine LIS(Array(3,2,6,4,5,1))
|
||||
WScript.StdOut.WriteLine LIS(Array(0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15))
|
||||
Loading…
Add table
Add a link
Reference in a new issue