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

using namespace std;

typedef long long ll;
#define fi first
#define se second
#define pb push_back
#define md ((st + nd) >> 1)
#define lc (1 + (idx << 1))
#define rc (2 + (idx << 1))
const int N = 200005;
char s[N];
int nxt[N], pre[N];

int main() {
	memset(nxt, -1, sizeof nxt);
	scanf("%s", s);
	int n = strlen(s);
	for (int i = n - 1; i >= 0; i--) {
		nxt[i] = nxt[i+1];
		if (s[i] == 'R') {
			nxt[i] = i;
		}
	}
	pre[0] = 1;
	for (int i = 1; i < n; i++) {
		pre[i] = pre[i-1] + (s[i] == 'N');
	}
	int cnt = 0;
	int lst = 1, res=   0;
	for (int i = 2; i < n; i++) {
		if (s[i] == 'R') {
			lst = cnt;
			cnt = 0;
		} else {
			cnt++;
			if (cnt > lst + lst) {
				int j = nxt[i];
				if (j == -1 || pre[j] - pre[i] >= (cnt - 1) * 2) {
					s[i] = 'R';
					res++;
					lst = cnt - 1;
					cnt = 0;
				}
			}
		}
	}
	printf("%d\n%s", res, s);
	return 0;
}
Copy
Never Gonna Give You Up aboAdnan
GNU G++17
9 ms
2.8 MB
Wrong Answer