Hello. I am trying to solve the problem 266A. I tested my code and it works fine locally. However, the judge is giving wrong answer. I don't know what is the cause of this. Also, when I outputted the value of R after breaking inner while loop, it's somehow 36 when it should theoretically be strictly less than value of n(for one of test case "RRG" that's length 3). I wasn't facing this issue when I ran code locally. What can be the problem? Here it is:
#include <iostream>
using namespace std;
void solve(){
int n;
cin >> n;
cout << "Value of n: " << n << '\n';
string s;
cin >> s;
int R, L = 0;
int ans = 0;
while(L < n - 1){
//encountered contiguous block of adjacent stones of same color => extend window far as possible of mathcing color!
if(s[L] == s[L+1]){
cout << "Found contiguous block!" << '\n';
while(R < n && s[R] == s[L]){
R++;
}
cout << "Cur Left Boundary: " << L << '\n';
cout << "End Right Boundary: " << R << '\n';
//R - L is number of stones adjacent of same color=> need to remove 1 less than it to satisfy requirement of problem =>
//no two adjacent stones same color!
ans += R - L - 1;
cout << "Value of ans after adding: " << ans << '\n';
L = R;
} else{
L++;
R++;
}
}
cout << ans;
}
int main(){
solve();
}
Thanks! Much help appreciated.
you have not initialized R, so it is taking random values.
use
int R=0, L = 0;