Source Code
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

int main(){
    FAST
    string s; cin >> s;
    int n = s.size();
    vector<int> R;
    for(int i = 0; i < n; i++){
        if(s[i] == 'R'){
            R.pb(i);
        }
    }
    int last = -1;
    int szLast = 0;
    int ans = 0;
    for(int i = 1; i < n; i++){
        if(s[i] == 'N'){
            int ind = upper_bound(R.begin(), R.end(), i) - R.begin();
            if(last == i - 1)continue;
            if(ind == R.size() || R[ind] != i + 1){
                if(last == -1){
                    ans++;
                    s[i] = 'R';
                    last = i;
                }
                else{
                    int currSz = i - last - 1;
                    int szAfter = 1e9;
                    if(ind != R.size()){
                        szAfter = R[ind] - i - 1;
                    }
                    if(currSz >= szLast*2 && szAfter >= 2*currSz){
                        s[i] = 'R';
                        szLast = currSz;
                        last = i;
                        ans++;
                    }
                }
            }
        }
        else{
            if(last != -1){
                szLast = i - last - 1;
            }
            last = i;
        }
    }
    cout << ans << endl << s << endl;
    return 0;
}
Copy
Never Gonna Give You Up Basilhijaz
GNU G++17
8 ms
1.5 MB
Accepted