SPOJ ABCD

Its a nice greedy problem to solve. Lets first start thinking that we are given a Na,Nb,Nc,Nd numbers to use A,B,C,D to create 2nd row. There is just one condition we cann't make adjacent's same charecter. So NOT TO make adjacent what should we do ? We have to distribute them as sparse as we can,right? And what charecters should we use in position i is the vital question...Should not be this ---

1.We should use most remaining charecter to distribute according to problem and 2.In case of tie --- we should use that charecter which is most used in FIRST row from Position (i...N) to make as far as possible same charecters.

If both cases Fail just check from (0...i-2) and take one which can fulfill all conditions in that position i;

Hope this helps.....