Skip to content

Instantly share code, notes, and snippets.

@asSqr
Created May 11, 2019 17:01
Show Gist options
  • Save asSqr/2befdd5beb2f2327a892a8aae4afb65d to your computer and use it in GitHub Desktop.
Save asSqr/2befdd5beb2f2327a892a8aae4afb65d to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <iostream>
#include <string>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define all(a) (a).begin(), (a).end()
int N;
int cnt[3];
int ans, dup, res;
int main()
{
std::cin >> N;
rep( i, N )
{
std::string S;
std::cin >> S;
rep( i, S.size()-1 )
{
if( S[i] == 'A' && S[i+1] == 'B' )
++ans;
}
bool fl = false;
if( S[0] == 'B' && S[S.size()-1] == 'A' )
++cnt[2];
else if( S[0] == 'B' )
++cnt[1];
else if( S[S.size()-1] == 'A' )
++cnt[0];
}
int p = 0;
if( cnt[0] > cnt[1] )
p = 1;
else
p = 0;
int both = 0;
while( cnt[0] != cnt[1] && cnt[2] )
{
if( cnt[2] > 0 )
++cnt[p], --cnt[2], ++both;
}
if( cnt[0] == cnt[1] )
{
// cnt[2]-1 when cnt[2]>both
// cnt[2] when both>=cnt[2]
res = cnt[0]+(both>=cnt[2]?cnt[2]:both>0?cnt[2]:cnt[0]>0?cnt[2]:cnt[2]-1);
}
else
res = std::min(cnt[0],cnt[1])+cnt[2];
std::cout << ans+res << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment