-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABBB.cpp
More file actions
65 lines (65 loc) · 1.69 KB
/
Copy pathABBB.cpp
File metadata and controls
65 lines (65 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
string s;
cin>>s;
vector<char>a;
for(ll i=0;i<s.size();i++){
a.push_back(s[i]);
}
bool flag=false;
while(flag==false){
vector<char>b;
flag=true;
for(ll i=0;i<a.size()-1;i++){
if(a[i]=='A' and a[i+1]=='B'){
ll x=i,y=i+1;
flag=false;
while(a[x]=='A' and a[y]=='B'){
a[x]='C';a[y]='C';
x--;y++;
if(x<0 or y>=a.size())break;
}
i=y;
}
}
for(ll i=0;i<a.size();i++){
if(a[i]!='C')b.push_back(a[i]);
}
if(b.begin()==b.end()){
a=b;
flag=true;
}
a=b;
}
if(a.begin()==a.end())cout<<0<<endl;
else{
flag=false;
while(flag==false){
vector<char>b;
flag=true;
for(ll i=0;i<a.size()-1;i++){
if(a[i]=='B' and a[i+1]=='B'){
a[i]='C';
a[i+1]='C';
flag=false;
}
}
for(ll i=0;i<a.size();i++){
if(a[i]!='C')b.push_back(a[i]);
}
if(b.begin()==b.end()){
a=b;flag=true;
}
a=b;
}
cout<<a.size()<<endl;
}
}
return 0;
}