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);
	if (n <= 3) {
		return printf("0\n%s", s), 0;
	}
	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, i = 4;
	int lst = 1, res = 0;
	if (nxt[3] == -1) {
		s[3] = 'R';
		res++;
	} else if (nxt[3] > 3) {
		if (pre[nxt[3]] - pre[3] >= 2) {
			res++;
			s[3] = 'R';
		} else {
			lst = pre[nxt[3]] - 1;
			i = nxt[3] + 1;
		}
	}
	for (; 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
18 ms
2.9 MB
Accepted