Skip to content

Instantly share code, notes, and snippets.

@asSqr
Created May 11, 2019 16:30
Show Gist options
  • Save asSqr/73221eea1733059f29ab33b4b43031a6 to your computer and use it in GitHub Desktop.
Save asSqr/73221eea1733059f29ab33b4b43031a6 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] )
res = cnt[0]+std::min(both,cnt[2]);
else
res = std::min(cnt[0],cnt[1])+std::min(both,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