Dynamic Programming and Greedy Algorithms

Algorithm:

A rod of length n can be cut into pieces, and a piece of length i sells for p[i]. Bottom-up, compute the best revenue r[j] for every length j ≤ n, remembering in s[j] the size of the first piece.

Fill in c[i, j], the length of an LCS of the first i characters of X and the first j of Y. The arrows record which case applied, and following them back from the corner spells out the LCS.

With activities sorted by finish time, repeatedly choose the first one that starts after the last chosen one finishes. This greedy choice gives the most compatible activities.

Speed:
Sample input:
Prices
Best revenue r[j] for a rod of length j
Size s[j] of the first piece to cut
Timeline