Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,28 @@
# Finds the subsequence of ary[1] to ary[len] with the greatest sum.
# Sets subseq[1] to subseq[n] and returns n. Also sets subseq["sum"].
# An empty subsequence has sum 0.
function maxsubseq(subseq, ary, len, b, bp, bs, c, cp, i) {
b = 0 # best sum
c = 0 # current sum
bp = 0 # position of best subsequence
bn = 0 # length of best subsequence
cp = 1 # position of current subsequence
for (i = 1; i <= len; i++) {
c += ary[i]
if (c < 0) {
c = 0
cp = i + 1
}
if (c > b) {
b = c
bp = cp
bn = i + 1 - cp
}
}
for (i = 1; i <= bn; i++)
subseq[i] = ary[bp + i - 1]
subseq["sum"] = b
return bn
}

View file

@ -0,0 +1,26 @@
# Joins the elements ary[1] to ary[len] in a string.
function join(ary, len, i, s) {
s = "["
for (i = 1; i <= len; i++) {
s = s ary[i]
if (i < len)
s = s ", "
}
s = s "]"
return s
}
# Demonstrates maxsubseq().
function try(str, ary, len, max, maxlen) {
len = split(str, ary)
print "Array: " join(ary, len)
maxlen = maxsubseq(max, ary, len)
print " Maximal subsequence: " \
join(max, maxlen) ", sum " max["sum"]
}
BEGIN {
try("-1 -2 -3 -4 -5")
try("0 1 2 -3 3 -1 0 -4 0 -1 -4 2")
try("-1 -2 3 5 6 -2 -1 4 -4 2 -1")
}