There is a thin, long and hollow fibre with a virus in the centre. The virus occasionally becomes active and secretes some side products. The fibre is so thin that new side products secreted by the virus push the old products along the fibre towards its ends. The possible actions of the virus are as follows
- Produce an acid molecule to its left and a base molecule to its right.
- Produce a base molecule to its left and an acid molecule to its right.
- Divide into two viruses, each of which continues to behave like its ancestor.
You are given a sequence of acid and base molecules from one end of the fibre to the other end. Design an algorithm to check if a single virus could possibly have produced the given sequence. Use dynamic programming, checking smaller subsequences before checking bigger subsequences.