-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABBC-or-BACB.cpp
62 lines (53 loc) · 928 Bytes
/
ABBC-or-BACB.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
void solve()
{
string s;
cin>>s;
ll n=s.length(),cnt=0;
bool all=(s[0]=='B' || s[n-1]=='B');
for(ll i=0;i<n-1;i++)
{
if(s[i]==s[i+1] && s[i]=='B')
all=true;
}
vector<ll> l;
for(int i=0;i<n;i++)
{
if(s[i]=='A')
{
cnt++;
}
else
{
if(cnt!=0)
{
l.push_back(cnt);
}
cnt=0;
}
}
if(cnt!=0) l.push_back(cnt);
sort(l.begin(),l.end());
if(l.empty())
{
cout<<0<<endl;
return;
}
ll tot=0;
if(all) tot+=l[0];
for(int i=1;i<l.size();i++)
tot+=l[i];
cout<<tot<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--)
{ solve(); }
return 0;
}