Help with problem bline

Hi, I tried the problem “bline”. My final solution is here: https://www.codechef.com/viewsolution/25962470. Can someone give me some insight regarding what is really wrong with my answer? I tested the corner cases like “BWB”, I wrote a random testing program that says my program is right when the input size is small (10^8 ~ 10^9). My computer doesn’t handle bigger strings. As you might see, I have been working on this for days, fixed many bugs and mistakes, but it is still wrong, and I don’t know why. I appreciate any kind of help! Please!