4

Don't Know Why Judge is Outputting Incorrect Answer When Local Program Works

 4 weeks ago
source link: https://codeforces.com/blog/entry/127643
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK