Problem Broken Necklace https://train.usaco.org/usacoprob2?a=db9DeXdtp4m&S=beads
After Submitting my Code it says my output is 12 and the correct answer is 11,
but if i try it on my own I also get 11.
Any Suggestion what I could be doing wrong?
My Code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string kette;
int dadad;
ofstream out ("beads.out");
ifstream in ("beads.in");
in >> dadad;
in >> kette;
kette = kette + kette;
int r = 0;
int b = 0;
char l = 0;
char ll = 0;
int tot = 0; int totMax = 0;
bool rb;
bool bb;
int r_b = 0;
int b_r = 0;
for(int i = 0; i < kette.length(); i++) {
if(l == 0 && ll == 0 && kette[i] != 'w') {
if(kette[i] == 'r') {
l = 'b';
ll = 'b';
}
if(kette[i] == 'b') {
l = 'r';
ll = 'r';
}
}
if(kette[i] == 'r') {
rb = 1;
bb = 0;
}
if(kette[i] == 'b') {
bb = 1;
rb = 0;
}
if(kette[i] == 'r') {
if(ll != l && l != 'r') {
r = 0;
ll = 'b';
}
l = 'r';
r++;
}
if(kette[i]== 'b') {
if(ll != l && l != 'b') {
b = 0;
ll = 'r';
}
l = 'b';
b++;
}
if(kette[i] == 'w') {
for(int j = i + 1; j < kette.length(); j++) {
if(kette[j] != 'w') {
if(!rb && !bb) {
if(kette[j] == 'r') {
r++;
break;
}
if(kette[j] == 'b') {
b++;
break;
}
}
if(rb) {
if(kette[j] == 'r') {
r++;
break;
}
if(kette[j] == 'b') {
r_b = 0;
r_b++;
break;
}
}
if(bb) {
if(kette[j] == 'b') {
b++;
break;
}
if(kette[j] == 'r') {
b_r = 0;
b_r++;
break;
}
}
}
}
}
tot = r + b + r_b + b_r; //r_b + b_r;
if(tot > totMax) totMax = tot;
}
if(totMax > dadad) totMax = dadad;
out << totMax << endl;
return 0;
}