Language: C++

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=1000005;
char s[N];
int n,top,q[N];
int cnt[3],id[3];
int pre[N],suf[N];
int b1,b2,c1,c2;
pii qq[N];
void work1(int i){
	if (b1<=c2) return;
	--cnt[id[1]]; --b1;
	suf[pre[i]]=suf[i];
	pre[suf[i]]=pre[i];
	qq[i]=pii(-2,0);
	if (qq[pre[i]]==pii(-1,1)&&qq[suf[i]]==pii(-1,1)){
		--cnt[id[0]];
		int x=pre[i];
		suf[pre[x]]=suf[x];
		pre[suf[x]]=pre[x];
		qq[x]=pii(-2,0);
	}
}
void work2(int i){
	if (c1<=b2) return;
	--cnt[id[2]]; --c1;
	suf[pre[i]]=suf[i];
	pre[suf[i]]=pre[i];
	qq[i]=pii(-2,0);
	if (qq[pre[i]]==pii(-1,1)&&qq[suf[i]]==pii(-1,1)){
		--cnt[id[0]];
		int x=pre[i];
		suf[pre[x]]=suf[x];
		pre[suf[x]]=pre[x];
		qq[x]=pii(-2,0);
	}
}
int main(){
	scanf("%s",s+1);
	n=strlen(s+1);
	q[++*q]=s[1]-'A';
	For(i,2,n)
		if (s[i]!=s[i-1])
			q[++*q]=s[i]-'A';
	For(i,1,*q) ++cnt[q[i]];
	For(i,0,2) id[i]=i;
	if (cnt[id[0]]>cnt[id[1]]) swap(id[0],id[1]);
	if (cnt[id[0]]>cnt[id[2]]) swap(id[0],id[2]);
	if (cnt[id[1]]>cnt[id[2]]) swap(id[1],id[2]);
	qq[0].fi=-1;
	For(i,1,*q)
		if (q[i]==id[0])
			qq[++top]=pii(-1,1);
		else{
			if (qq[top].fi==-1)
				qq[++top]=pii(q[i],0);
			++qq[top].se;
		}
	b1=0,b2=0,c1=0,c2=0;
	For(i,1,top){
		if (qq[i].fi!=-1)
			if (qq[i].se>=2)
				++b2,++c2;
			else if (qq[i].fi==id[1])
				++b1,++b2;
			else if (qq[i].fi==id[2])
				++c1,++c2;
	}
	//printf("%d %d %d\n",id[0],id[1],id[2]);
	//printf("%d %d %d %d\n",b1,b2,c1,c2);
	qq[0]=pii(-2,0);
	For(i,1,top) pre[i]=i-1,suf[i]=i+1;
	suf[top]=0;
	if (qq[1  ]==pii(id[1],1)) work1(1);
	if (qq[top]==pii(id[1],1)) work1(top);
	For(i,2,top-1) if (qq[i]==pii(id[1],1)) work1(i);
	if (qq[1  ]==pii(id[2],1)) work2(1);
	if (qq[top]==pii(id[2],1)) work2(top);
	For(i,2,top-1) if (qq[i]==pii(id[2],1)) work2(i);
	if (cnt[id[1]]>cnt[id[2]]
	For(i,1,top){
		if (cnt[id[2]]>cnt[id[1]])
			if (qq[i].fi>=0&&(qq[i].se>1||(qq[i].se==1&&(!suf[i]||!pre[i]))))
				if (qq[i].fi==id[2]){
					qq[i].fi=id[1];
					--qq[i].se;
					--cnt[id[2]];
				}
		if (cnt[id[2]]>cnt[id[1]])
			if (qq[i].fi>=0&&(qq[i].se>1||(qq[i].se==1&&(!suf[i]||!pre[i]))))
				if ((qq[i].fi==id[2])^((qq[i].se+1)&1)){
					--qq[i].se;
					--cnt[id[2]];
				}
	}
	//printf("%d %d %d\n",cnt[id[0]],cnt[id[1]],cnt[id[2]]);
	//For(i,1,top)
	//	printf("%d %d\n",qq[i].fi,qq[i].se);
	For(i,1,top) for (;;){
		if (cnt[id[1]]<=cnt[id[0]]) break;
		if (qq[i].fi<0||qq[i].se<=2) break;
		qq[i].se-=2; cnt[id[1]]--; cnt[id[2]]--;
	}
	For(i,1,top)
		if (qq[i].fi==-1)
			putchar('A'+id[0]);
		else if (qq[i].fi>=0){
			int x=qq[i].fi;
			For(j,1,qq[i].se){
				putchar('A'+x);
				x=id[1]+id[2]-x;
			}
		}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:92:34: error: expected ')' before 'for'
   92 |         if (cnt[id[1]]>cnt[id[2]]
      |            ~                     ^
      |                                  )
a.cc:93:13: error: 'i' was not declared in this scope
   93 |         For(i,1,top){
      |             ^
a.cc:10:40: note: in definition of macro 'For'
   10 | #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
      |                                        ^