Reconstructing the Answer & the Edit Script
In short: a DP table's final cell only gives a number. To get the actual
answer — which items were kept, inserted or deleted — walk back from that cell
to (0, 0), replaying at each step which neighbour it was built from: a
diagonal match is kept, otherwise the walk goes left (insert) or up (delete)
depending on which neighbour holds the table's value. The walk runs backwards,
so reverse the emitted list at the end. Hirschberg's trick gets the same path
in O(n) space by never keeping the whole table at all.

The survival score says 4 of 5 lines survived, but not which 4, and a history panel that can't show what changed isn't much use. Quarry needs an edit script: an ordered list of keep, delete and insert that turns the old version into the new one.
Every number in a DP table was chosen from a neighbour. Walk those choices backwards from the final cell and you recover the answer itself, not just its size, and with one more idea you can do it without storing the table.
Walking back from the corner
Take the LCS table from lesson 2, for CRATE against TRACE. Start at the
bottom-right cell (i, j) = (5, 5) and repeat until you reach (0, 0):
- If
a[i-1] == b[j-1], that item is kept. Emit=and step diagonally. - Otherwise, if moving left keeps the same value (
table[i][j-1] >= table[i-1][j]), thenb[j-1]was inserted. Emit+and step left. - Otherwise
a[i-1]was deleted. Emit-and step up. - Once
iorjreaches 0, only one direction is left: the remaining items ofbare inserts, or the remaining items ofaare deletes.
The walk emits the script backwards, so reverse it at the end.
Walking back from the corner replays every choice the fill made. Diagonals are kept items, left steps are inserts and up steps are deletes.
Start in the corner, which holds 3. E equals E, so E was kept. Emit = E and step diagonally. 6 by 6 table. At row 5, column 5 — value 3. 36 of 36 cells filled.
The walk visits at most m + n cells, so it costs O(m + n) on top of the fill.
Filling the table is the same O(m · n) time as lesson 2, and keeping the
whole table to walk back over it costs O(m · n) space — the price this
lesson pays for the extra arrows, until Hirschberg's trick below.
The expensive part is keeping the table.
Step through a tiny walk and watch i and j fall:
old, new = ["a", "b"], ["b", "c"]
t = [[0, 0, 0], [0, 0, 0], [0, 1, 1]]
script, i, j = [], 2, 2
while i > 0 or j > 0:
if i > 0 and j > 0 and old[i - 1] == new[j - 1]:
script.append("=" + old[i - 1])
i, j = i - 1, j - 1
elif j > 0 and (i == 0 or t[i][j - 1] >= t[i - 1][j]):
script.append("+" + new[j - 1])
j -= 1
else:
script.append("-" + old[i - 1])
i -= 1
print(script[0])Why ties go left
A replaced line is a delete plus an insert, and both orders are equally short. Which one you get depends only on how you break the tie. Walking backwards and preferring the insert means the insert is emitted first, so after the reverse it comes second. That gives the order every diff reader expects: the old line, then its replacement.
old, new = ["shards=2"], ["shards=4"]
t = [[0, 0], [0, 0]]
out, i, j = [], 1, 1
while i > 0 or j > 0:
if i > 0 and j > 0 and old[i - 1] == new[j - 1]:
out.append("=" + old[i - 1]); i -= 1; j -= 1
elif j > 0 and (i == 0 or t[i][j - 1] >= t[i - 1][j]):
out.append("+" + new[j - 1]); j -= 1
else:
out.append("-" + old[i - 1]); i -= 1
print(out[::-1])Here's a walk with the wrong loop condition:
Turning [a, b] into [b] deletes a, but the script leaves that out because the loop stops as soon as one index hits 0. Make the walk run until both are 0.