Efficient optimal decomposition of a sequence into disjoint regions, each matched to some template in an inventory.