Skip to content

Commit 64a801c

Browse files
committed
search bugfix: certain optimal bruteforce matches are no longer ignored
1 parent 2db267c commit 64a801c

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/scoring.coffee

+15-17
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,24 @@ scoring =
120120
optimal.m[k][l] = m
121121
optimal.pi[k][l] = pi
122122

123-
# helper: considers whether bruteforce matches ending at position k are optimal.
124-
# three cases to consider...
123+
# helper: evaluate bruteforce matches ending at k.
125124
bruteforce_update = (k) =>
126-
# case 1: a bruteforce match spanning the full prefix.
125+
# see if a single bruteforce match spanning the k-prefix is optimal.
127126
m = make_bruteforce_match(0, k)
128127
update(m, 1)
129-
return if k == 0
130-
for l, last_m of optimal.m[k - 1]
131-
l = parseInt(l) # note: js stores object keys as strings
132-
if last_m.pattern == 'bruteforce'
133-
# case 2: if the optimal length-l sequence up to k - 1 ended in a bruteforce match,
134-
# consider whether extending it by one character is optimal up to k.
135-
# this preserves the sequence length l.
136-
m = make_bruteforce_match(last_m.i, k)
137-
update(m, l)
138-
else
139-
# case 3: if the optimal length-l sequence up to k - 1 ends in a non-bruteforce match,
140-
# consider whether starting a new single-character bruteforce match is optimal.
141-
# this adds a new match, adding 1 to the prior sequence length l.
142-
m = make_bruteforce_match(k, k)
128+
for i in [1..k]
129+
# generate k bruteforce matches, spanning from (i=1, j=k) up to (i=k, j=k).
130+
# see if adding these new matches to any of the sequences in optimal[i-1]
131+
# leads to new bests.
132+
m = make_bruteforce_match(i, k)
133+
for l, last_m of optimal.m[i-1]
134+
l = parseInt(l)
135+
# corner: an optimal sequence will never have two adjacent bruteforce matches.
136+
# it is strictly better to have a single bruteforce match spanning the same region:
137+
# same contribution to the guess product with a lower length.
138+
# --> safe to skip those cases.
139+
continue if last_m.pattern == 'bruteforce'
140+
# try adding m to this length-l sequence.
143141
update(m, l + 1)
144142

145143
# helper: make bruteforce match objects spanning i to j, inclusive.

0 commit comments

Comments
 (0)