Source Code
#include <bits/stdc++.h>
using namespace std;
    
int main (){

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

	string s;
	cin >> s;
	int n = s.size();
	int p = 0, o = 0, ans = 0;
	set<int> st;
	for (int i = 0; i < n; ++i) {
		if (s[i] == 'R') {
			st.insert(i);
		}
	}
	for (int i = 1; i < n; ++i) {
		if (s[i] == 'N') {
			if (o && o >= 2 * p && (st.lower_bound(i) == st.end() || (*st.lower_bound(i) - i) >= 2 * o)) {
				p = o;
				o = 0;
				++ans;
				s[i] = 'R';
				--o;
			}
			++o;
		}
		else {
			p = o;
			o = 0;
		}
	}
	cout << ans << "\n" << s;

  return 0;
}
Copy
Never Gonna Give You Up SuhaibSawalha1
GNU G++17
6 ms
1.5 MB
Accepted