UVa 10981 - String Morphing

Summary
Determine the steps to morph a string to a character by a set of "grammar", subject to certain requirements.

Explanation
There is an obvious $$O(n^3)$$ algorithm to check if a string can be morphed to our desired character (such an algorithm is known as CYK algorithm). Based on this we can get a simple $$O(n^4)$$ algorithm to meet the requirements.

Input
3 bbbba a bbbba b bbbba c

Output
bbbba bbba bba bc a

None exist!

bbbba bbba bba ba c