I want to align two DNA sequences in an optimal way, but I have the gap penalty function of length L, that if L is a multiple of 3, the penalty is a * L for some constant a. If L is not a multiple of 3, then the penalty is b * L for some constant b.
I am supposed to design a O(n * m) algorithm, where n and m are lengths of DNA sequences, that finds the optimal alignment. But the tricky part about this is that I have to keep track of the size of the gap it is extending. For example, if I have two contiguous gaps and extend one gap further, I would need to update the score by aL - b(L-1), but I couldn't formulate subproblems that handle this situation well. I've thought about introducing a new parameter L for "guessing" the length of the final gap, but that would easily exceeds O(n * m).
Is there a way to formulate these subproblems effectively? Any key observations would be greatly appreciated.