# AtCoder Grand Contest 036E

$A$的数量为$p$,$B$的数量为$q$,$C$的数量为$r$,$p\leq q\leq r$

• $b$表示形如$A..B..A$的串的数量.
• $c$表示形如$ACA$的串的数量.
注意这些串除了开头结尾均不含$A$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<bitset>
#include<set>
#define N (5000010)
#define P ()
#define M ()
#define inf (0x7f7f7f7f)
#define rg register int
#define Label puts("NAIVE")
#define spa print(' ')
#define ent print('\n')
#define rand() (((rand())<<(15))^(rand()))
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef long double ld;
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
int n,c1,c2,c3,p,q,now,tar;
char s[N],str[N],w1,w2,w3;
vector<int> pos2; bool ban[N];
int gs(int x){
if(s[x]==w1)return 1;else if(s[x]==w2)return 2;else return 3;
}
void getstr(int L,int R){
if(L>R)return;
int ch[4]={0,0,0,0};
for(int i=L;i<=R;i++)ch[gs(i)]++;
if(!ch[2]&&L>1&&R<n)q++,pos2.push_back(L-1);
if(ch[2])p++;
}
void del(){
int tmp=0,lst=-1;
for(int i=1;i<=n;i++)
if(!ban[i]&&s[i]!=lst)lst=s[i],s[++tmp]=s[i];
n=tmp;
}
void cal(){
c1=c2=c3=0;
for(int i=1;i<=n;i++)
if(s[i]==w1)c1++;else if(s[i]==w2)c2++;else c3++;
memset(ban,0,sizeof(ban));
}
void solve(int L,int R){
if(L>R)return;
if(L==R){
if(L==1||L==n){if(s[L]==w3&&c3>c2)c3--,ban[L]=1;}
return;
}
if(s[L]==w3&&c3>c2)c3--,ban[L]=1;
if(s[R]==w3&&c3>c2)c3--,ban[R]=1;
}
void solve2(int L,int R){
if(L>R)return;
while(L<R&&now>tar){
if(L+1>=R&&L!=1&&R!=n)break;
now--,ban[L]=ban[L+1]=1,L+=2;
}
}
int main(){
scanf("%s",str+1),w1='A',w2='B',w3='C';
for(int i=1;i<=strlen(str+1);i++)
if(str[i]!=str[i-1]){
s[++n]=str[i];
if(s[n]==w1)c1++;else if(s[n]==w2)c2++;else c3++;
}
if(c1>c2)swap(c1,c2),swap(w1,w2);
if(c1>c3)swap(c1,c3),swap(w1,w3);
if(c2>c3)swap(c2,c3),swap(w2,w3);
int j=0;
for(int i=1;i<=n;i++)
if(s[i]==w1)getstr(j+1,i-1),j=i; getstr(j+1,n);
if(p<q)for(int i=0;i<q-p;i++)ban[pos2[i]]=1,c1--;
del(),cal(),j=0;
for(int i=1;i<=n;i++)
if(s[i]==w1)solve(j+1,i-1),j=i; solve(j+1,n);
del(),cal(),j=0,now=c2,tar=c1;
for(int i=1;i<=n;i++)
if(s[i]==w1)solve2(j+1,i-1),j=i; solve2(j+1,n);
for(int i=1;i<=n;i++)
if(!ban[i])printf("%c",s[i]);
}

posted @ 2019-10-16 15:38  Romeolong  阅读(57)  评论(2编辑  收藏