interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / longest-increasing-subsequence.wiki

160 lines · 3046 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text minus "-"
9text d1 "3 2 6 4 5 1"
10text d2 "0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15"
11global vals long 16
12global ln ulong 16
13global nx ulong 16
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
41fn emiti n long to void
42if lt n 0
43emit minus
44emitu add bitcast ulong not n 1
45else
46emitu ulong n
47end
48end
49note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
50note returns how many; one pass over s, so the loop is bounded by len s
51fn parse s view uchar * to ulong
52n = ulong 0
53v = long 0
54sg = long 1
55dig = false
56for i ulong len s
57c = get s i
58if eq c 45
59sg = long -1
60else
61if and ge c 48 le c 57
62v = add mul v 10 long sub c 48
63dig = true
64else
65if dig
66put vals n mul sg v
67n = add n 1
68end
69v = long 0
70sg = long 1
71dig = false
72end
73end
74end
75if dig
76put vals n mul sg v
77n = add n 1
78end
79ret n
80end
81note show writes vals[0..n-1] separated by spaces, then a newline
82fn show n ulong to void
83for i ulong n
84if gt i 0
85emit sp
86end
87emiti get vals i
88end
89emit nl
90end
91note lis prints the lexicographically smallest of the longest strictly increasing subsequences:
92note ln[i] is the longest increasing run starting at i (computed right to left), nx[i] the smallest
93note next value that keeps it longest; the walk then starts at the smallest value with the maximum ln
94note grow considers continuing the run at i with element k > i
95fn grow i ulong k ulong to void
96if gt get vals k get vals i
97c = add get ln k 1
98if gt c get ln i
99put ln i c
100put nx i k
101else
102if eq c get ln i
103if lt get vals k get vals get nx i
104put nx i k
105end
106end
107end
108end
109end
110note better says whether a run starting at i beats the best so far (length m starting at s)
111fn better i ulong s ulong m ulong to bool
112if gt get ln i m
113ret true
114end
115ret and eq get ln i m lt get vals i get vals s
116end
117fn lis n ulong to void
118for x ulong n
119i = sub sub n x 1
120put ln i 1
121put nx i n
122for j ulong sub sub n i 1
123grow i add add i j 1
124end
125end
126m = ulong 0
127s = ulong 0
128for i ulong n
129if better i s m
130m = get ln i
131s = i
132end
133end
134i = s
135for x ulong m
136if gt x 0
137emit sp
138end
139emiti get vals i
140i = get nx i
141end
142emit nl
143end
144fn main to int
145n = parse d1
146show n
147lis n
148n = parse d2
149show n
150lis n
151ret 0
152end
153</syntaxhighlight>
154{{out}}
155<pre>
1563 2 6 4 5 1
1572 4 5
1580 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15
1590 2 6 9 11 15
160</pre>