Language: C++

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;x++)
#define Red(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
const int MAXN=1e6+5;
char buf[1<<12],*pp1=buf,*pp2=buf,nc;int ny;
inline char gc() {return pp1==pp2&&(pp2=(pp1=buf)+fread(buf,1,1<<12,stdin),pp1==pp2)?EOF:*pp1++;}
//inline char gc(){return getchar();}
inline int read(){
	int x=0;for(ny=1;nc=gc(),(nc<48||nc>57)&&nc!=EOF;)if(nc==45)ny=-1;if(nc<0)return nc;
	for(x=nc-48;nc=gc(),47<nc&&nc<58&&nc!=EOF;x=(x<<3)+(x<<1)+(nc^48));return x*ny;
}
int l[MAXN],r[MAXN],cnt[3],a[MAXN],n,c[3],b[3];char s[MAXN];
inline void Del(int x){cnt[a[x]]--,r[l[x]]=r[x],l[r[x]]=l[x];}
inline bool cmp(int x,int y){return cnt[x]<cnt[y];}
inline void Sort(){Rep(i,0,2)c[i]=i;sort(c,c+3,cmp);}
vector<int>S;
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	scanf("%s",s+1),n=strlen(s+1),a[0]=a[n+1]=-1;
	Rep(i,0,n)l[i]=i-1,r[i]=i+1;
	Rep(i,1,n){cnt[a[i]=s[i]-'A]++;if(a[i]==a[i-1])Del(i);}Sort();
	for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[l[i]]!=a[r[i]])S.push_back(i);
	for(int i=0;i<S.size()&&cnt[c[2]]!=cnt[c[1]];i++)Del(S[i]);S.clear();
	for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[l[i]]==c[0]&&a[r[i]]==c[0])S.push_back(i);
	for(int i=0;i<S.size()&&cnt[c[2]]!=cnt[c[1]];i++)Del(S[i]),Del(r[S[i]]);S.clear();
	for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[r[i]]==c[1])S.push_back(i);
	for(int i=0;i<S.size()&&cnt[c[1]]!=cnt[c[0]];i++)if(l[S[i]]!=c[0]||r[r[S[i]]]!=c[0])Del(S[i]),Del(r[S[i]]);S.clear();
	for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[1]&&a[r[i]]==c[2])S.push_back(i);
	for(int i=0;i<S.size()&&cnt[c[1]]!=cnt[c[0]];i++)if(l[S[i]]!=c[0]||r[r[S[i]]]!=c[0])Del(S[i]),Del(r[S[i]]);S.clear();
	if(cnt[0]==cnt[1]&&cnt[1]==cnt[2])for(int i=r[0];i!=n+1;i=r[i])putchar(s[i]);
	return 0;
}




Compiler output:
a.cc:30:34: warning: missing terminating ' character
   30 |         Rep(i,1,n){cnt[a[i]=s[i]-'A]++;if(a[i]==a[i-1])Del(i);}Sort();
      |                                  ^
a.cc:30:34: error: missing terminating ' character
   30 |         Rep(i,1,n){cnt[a[i]=s[i]-'A]++;if(a[i]==a[i-1])Del(i);}Sort();
      |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:31:9: error: expected primary-expression before 'for'
   31 |         for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[l[i]]!=a[r[i]])S.push_back(i);
      |         ^~~
a.cc:30:34: error: expected ']' before 'for'
   30 |         Rep(i,1,n){cnt[a[i]=s[i]-'A]++;if(a[i]==a[i-1])Del(i);}Sort();
      |                                  ^
      |                                  ]
   31 |         for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[l[i]]!=a[r[i]])S.push_back(i);
      |         ~~~                       
a.cc:31:37: error: expected ';' before ')' token
   31 |         for(int i=r[0];i!=n+1;i=r[i])if(a[i]==c[2]&&a[l[i]]!=a[r[i]])S.push_back(i);
      |                                     ^
      |                                     ;
a.cc:41:2: error: expected '}' at end of input
   41 | }
      |  ^
a.cc:25:11: note: to match this '{'
   25 | int main(){
      |           ^