SPOJ dp problem COLORSEG

Can anyone help me with this problem?

It seems it requires some state space reduction/some clever optimisation or an entirely different approach.Have been trying this for quite a long time and no solution/hints are available anywhere.Would be grateful if anyone can help

You may use the approach mentioned in this link.
Happy Coding :slight_smile: