Problem:
 Score : 400 points    Problem Statement Snuke has N strings. The i -th string is s_i .  Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.      Constraints  1 \leq N \leq 10^{4}  2 \leq |s_i| \leq 10  s_i consists of uppercase English letters.         Input Input is given from Standard Input in the following format:  N  s_1  \vdots  s_N       Output Print the answer.        Sample Input 1 3
ABCA
XBAZ
BAD      Sample Output 1 2  For example, if we concatenate ABCA , BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB .       Sample Input 2 9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA      Sample Output 2 4       Sample Input 3 7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA      Sample Output 3 4

Solution (Python):
N = int(input())
s = []
for _ in range(N):
    s.append(input())

in_str = 0
BX = 0
XA = 0
BA = 0
for str in s:
    in_str += str.count('AB')
    if str[0] == 'B' and str[-1] != 'A':
        BX += 1
    elif str[0] != 'B' and str[-1] == 'A':
        XA += 1
    elif str[0] == 'B' and str[-1] == 'A':
        BA += 1

ans = in_str
if min(XA, BX) >= 1:
    ans += BA + min(XA, BX)
elif max(BX, XA) == 0:
    ans += max(0, BA - 1)
else:
    ans += 1 + BA - 1
print(ans)

Solution (Python):
n=int(input())
a_num=0
b_num=0
set_num=0
ans=0
for i in range(n):
    s=list(input())
    for j in range(len(s)-1):
        if s[j]+s[j+1]=="AB":
            ans+=1
    if s[0]=="B" and s[-1]=="A":
        set_num+=1
    elif s[0]=="B":
        b_num+=1
    elif s[-1]=="A":
        a_num+=1
if set_num==0:
    print(ans+min(a_num,b_num))
else:
    ans+=set_num-1
    if not(a_num==0 and b_num==0):
        a_num+=1
        b_num+=1
    print(ans+min(a_num,b_num))

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]
res = 0
a = 0
b = 0
ba = 0
for s in S:
    if s[0] == "B" and s[-1] == "A":
        ba += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            res += 1
if a == 0 and b == 0:
    res += max(ba - 1, 0)
elif a == N and b == N:
    res += N-1 + ba
else:
    res += min(a, b) + ba
print(res)

Solution (Python):
N=int(input())
A=0
B=0
C=0
ans=0
for _ in range(0,N):
    s=input()
    if s[0]=='B':
        B+=1
    if s[-1]=='A':
        A+=1
    if s[-1]=='A' and s[0]=='B':
        C+=1

    for i in range(0,len(s)-1):
        if s[i]=='A' and s[i+1]=='B':
            ans+=1

if A!=C or B!=C:
    print(ans+min(A,B))
else:
    if A==0:
        print(ans)
    else:
        print(ans+C-1)

Solution (Python):
import sys
N = int(sys.stdin.readline())
ss = []
for i in range(N):
    ss.append(sys.stdin.readline().rstrip())

incount = 0
has_ba = 0
has_b = 0
has_a = 0
for s in ss:
    incount += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        has_ba += 1
    elif s[0] == 'B':
        has_b += 1
    elif s[-1] == 'A':
        has_a += 1

res = incount
if has_a:
    res += has_ba
elif has_ba:
    has_a += 1
    has_ba -= 1
    res += has_ba
res += min(has_a, has_b)















print(res)

Solution (Python):
n=int(input())
s=[ str(input()) for _ in range(n)]
ans=0
a=0
b=0
ab=0
for i in range(n):
    for j in range(len(s[i])-1):
        if s[i][j:j+2]=="AB":
            ans+=1

for i in range(n):
    if s[i][0]=="B" and s[i][-1]=="A":
        ab+=1
    elif s[i][0]=="B":
        b+=1
    elif s[i][-1]=="A":
        a+=1

ma=max(a,b)
mi=min(a,b)
if 1<=a :
    ans+=ab
else:
    if 1<=ab:
        a+=1
        ab-=1
        ans+=ab

ans+=min(a,b)


print(ans)

Solution (Python):
n = int(input())
s = []
for i in range(n):
    s.append(input())
out = 0
a=0
b=0
c=0
for i in range(len(s)):
    if s[i][0] == "B" and s[i][len(s[i])-1] == "A":
        a=a+1
    elif s[i][0] == "B" and s[i][len(s[i])-1] != "A":
        b=b+1
    elif s[i][0] != "B" and s[i][len(s[i])-1] == "A":
        c=c+1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            out = out+1
if a== 0 and b == 0 and c == 0:
    out = out
elif b == 0 and c == 0:
    out = out + a - 1
else :
    out = out +a+ min(b,c)
    
print(int(out))

Solution (Python):
n = int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[-1] == "A":
        if s[0] == "B":
            ba += 1
        a += 1
    if s[0] == "B":
        b += 1
if a > b:
    ans += b
elif b > a:
    ans += a
else:
    ans += a
    if a == ba and a > 0:
        ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]
ans = 0
ba, __a, b__ = 0, 0, 0
for i in range(N):
  ans += S[i].count("AB")
  if S[i][0] != "B" and S[i][-1] == "A":
    __a += 1
  elif S[i][0] == "B" and S[i][-1] == "A":
    ba += 1
  elif S[i][0] == "B" and S[i][-1] != "A":
    b__ += 1
if ba > 0:
  ans += ba - 1
  if __a > 0:
    ans += 1
    __a -= 1
  if b__ > 0:
    ans += 1
    b__ -= 1
ans += min(b__, __a)
print(ans)

Solution (Python):
import sys
def ii(): return int(sys.stdin.readline())

N = ii()
S = [sys.stdin.readline().rstrip() for _ in range(N)]
ans = 0
count = [0, 0, 0]
for s in S:
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        count[0] += 1
    elif s[0] == "B":
        count[1] += 1
    elif s[-1] == "A":
        count[2] += 1
if count[0]:
    ans += count[0] - 1
    if count[1]:
        ans += 1
        count[1] -= 1
    if count[2]:
        ans += 1
        count[2] -= 1
ans += min(count[1:])
print(ans)

Solution (Python):
N=int(input())
count=0
LastA=0
FirstB=0
LA=0
for i in range(0,N):
    f=input()
    count=count+f.count('AB')
    if f[0]=='B' and f[-1]=='A':
        LA=LA+1
    elif f[0]=='B':
        FirstB=FirstB+1
    elif f[-1]=='A':
        LastA=LastA+1
count=count+LA
if LastA==0 and FirstB==0 and LA!=0:
    count=count-1
elif FirstB>=LastA:
    count=count+LastA
else:
    count=count+FirstB
print(count)

Solution (Python):
n = int(input())
s= [input() for i in range(n)]
ans1=0
firstB=0
lastA=0
m=n
for i in s:
    ans1+=i.count("AB")
    if i[0]=="B":
        firstB+=1
    if i[-1]=="A":
        lastA+=1
    if i[0]!="B" and i[-1]!="A":
        m-=1
if min(firstB,lastA)==m:
    if m==0:
        print(ans1)
    else:
        print(ans1+m-1)
else:
    print(ans1+min(firstB,lastA))

Solution (Python):
n = int(input())
ans = 0
c_1 = 0
c_2 = 0
c_3 = 0
for _ in range(n):
    tmp = input()
    for i in range(len(tmp)-1):
        if tmp[i:i+2] ==  'AB':
            ans += 1
    if tmp[0] == 'B' and tmp[-1] == 'A':
        c_1 += 1
    elif tmp[0] == 'B':
        c_2 += 1
    elif tmp[-1] == 'A':
        c_3 += 1
if c_1 == 0:
    ans += min(c_2,c_3)
else:
    if c_2 + c_3 > 0:
        ans += c_1 + min(c_2,c_3)
    else:
        ans += c_1 - 1
print(ans)

Solution (Python):
N = int(input())
s = [0]* N
Middle = 0
Bcount = 0
Acount = 0
ABcount = 0
ans = 0
for i in range(N):
    s[i] = input()
    if s[i][0] == "B" and s[i][-1] == "A":
        ABcount += 1
    elif s[i][0] == "B":
        Bcount += 1
    elif s[i][-1] == "A":
        Acount += 1
    for j in range(len(s[i])-1):
        if s[i][j] == "A" and s[i][j+1] == "B":
            Middle += 1
if ABcount==0 and Acount==0 and Bcount==0:
    ans = Middle
elif Acount == 0 and Bcount==0:
    ans = Middle + ABcount - 1
elif Acount == 0:
    ans = Middle + ABcount
elif Bcount == 0:
    ans = Middle + ABcount
else:
    ans = Middle + ABcount +1 + min(Acount-1,Bcount-1)
print(ans)

Solution (Python):
N = int(input())
ans = 0
f = False
xx = 0
xA = 0
Bx = 0
BA = 0
A = 0
B = 0
for i in range(N):
  S = input()
  for i in range(len(S) - 1):
    if S[i] == "A" and S[i + 1] == "B":
      ans += 1
  if S[0] == "B":
    if S[-1] == "A":BA += 1
    else:Bx += 1
  else:
    if S[-1] == "A":xA += 1
if xA != 0 and Bx != 0:
  ans += BA + 1
  xA -= 1
  Bx -= 1
elif xA != 0:
  ans += BA
  xA -= 1
elif Bx != 0:
  ans += BA
  Bx -= 1
else:
  if BA >= 2:
    ans += BA - 1
ans += min(Bx,xA)
print(ans)

Solution (Python):
n = int(input())
S = [input() for i in range(n)]

ans = 0
a = 0
b = 0
c = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
    if S[i][-1] == "A" and S[i][0] == "B":
        c += 1

ans += min(a, b)
if a == c and a == b and a > 0:
    ans -= 1

print(ans)

Solution (Python):
n = int(input())
a = b = ab = ans = 0
for _ in range(n):
  t = input()
  ans += t.count("AB")
  if t[0] == "B":
    if t[-1] == "A":
      ab +=1
    else:
      b += 1
  elif t[-1] =="A":
    a += 1
if ab > 0:
  ans += ab-1
  if a > 0:
    ans += 1
    a -= 1
  if b > 0:
    ans += 1
    b -= 1
print(ans + min(a,b))

Solution (Python):


import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  







N = int(input())
cnt = end_A = start_B = both = 0
for _ in ra(N):
    s = input()
    cnt += s.count('AB')

    if s.endswith('A') and s.startswith('B'):
        both += 1
    elif s.endswith('A'):
        end_A += 1
    elif s.startswith('B'):
        start_B += 1











add = min(start_B, end_A)
if both > 0:
    if end_A == 0 and start_B == 0:
        add += both - 1
    else:
        add += both


print(cnt + add)

Solution (Python):
def count_AB(s):
    res = 0
    for j in range(len(s) - 1):
        if s[j] == 'A' and s[j + 1] == 'B':
            res += 1
    return res


N = int(input())
sA, BsA, Bs = (0, 0, 0)
ans = 0
for i in range(N):
    s = input()
    if s[0] == 'B' and s[-1] == 'A':
        BsA += 1
        if len(s[1:-1]) > 0:
            ans += count_AB(s[1:-1])
    elif s[0] == 'B':
        Bs += 1
        if len(s[1:]) > 0:
            ans += count_AB(s[1:])
    elif s[-1] == 'A':
        sA += 1
        if len(s[:-1]) > 0:
            ans += count_AB(s[:-1])
    else:
        ans += count_AB(s)

if BsA == 0:
    ans += min(sA, Bs)
else:
    if sA + Bs == 0:
        ans += BsA - 1
    else:
        ans += BsA
        ans += min(sA, Bs)

print(ans)

Solution (Python):
N = int(input())
S = []
g1 = []
g2 = []
g3 = []

ans = 0
for _ in range(N):
    s = input()
    ans += s.count("AB")
    if s[0] == 'B' and s[-1] == 'A':
        g1.append(s)
    if s[0] == 'B' and s[-1] != 'A':
        g2.append(s)
    if s[0] != 'B' and s[-1] == 'A':
        g3.append(s)



if len(g1) == 0:
    ans += min(len(g2), len(g3))
    print(ans)


else:
    if len(g2) + len(g3) == 0:
        
        ans += len(g1) - 1
    else:
        
        
        
        
        ans += len(g1) + min(len(g2), len(g3))
        pass
    print(ans)

Solution (Python):
N=int(input())
s=[input() for _ in range(N)]
abcount=0
righta=0
leftb=0
bothab=0
buf=""
for i in range(N):
    buf=s[i]
    l=len(buf)
    if (buf[0]=="B")and(buf[l-1]=="A"):
        bothab=bothab+1
    elif (buf[0]=="B"):
        leftb=leftb+1
    elif (buf[l-1]=="A"):
        righta=righta+1
    for j in range(l-1):
        if (buf[j]=="A")and(buf[j+1]=="B"):
            abcount=abcount+1
abcount=abcount+max(0,bothab-1)

if (righta>0)and(bothab>0):
    abcount=abcount+1
    righta=righta-1
if (leftb>0)and(bothab>0):
    abcount=abcount+1
    leftb=leftb-1
abcount=abcount+min(righta,leftb)

'''
abcount=abcount+min(bothab,righta,leftb)*2


if min(bothab,righta,leftb)==bothab:
    leftb=leftb-bothab
    righta=righta-bothab
    abcount=abcount+min(leftb,righta)
elif min(bothab,righta,leftb)==righta:
    bothab=bothab-righta
    leftb=leftb-righta
    abcount=abcount+max(0,bothab-1)
    if leftb>0:
        abcount=abcount+1
else:
    bothab=bothab-leftb
    righta=righta-leftb
    abcount=abcount+max(0,bothab-1)
    if righta>0:
        abcount=abcount+1
'''
print(abcount)

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
ab = 0
ba = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B" and s[i][-1] == "A":
    ba += 1
  elif s[i][-1] == "A":
    last_a += 1
  elif s[i][0] == "B":
    first_b += 1
  ab += s[i].count("AB")
if last_a == 0 and first_b == 0:
  print(ab+ba-1) if ba != 0 else print(ab)
else:
  print(ab+ba+min(last_a,first_b))

Solution (Python):
N=int(input())
s=[]
for i in range(N):
    S=input()
    s.append(S)

sm=0

for i in range(N):
    count=0
    tmp=-1
    for j in range(len(s[i])):
        if s[i][j]=="A":
            tmp=1
        elif s[i][j]=="B":
            if tmp==1:
                count+=1
            tmp=-1
        else:
            tmp=-1
    sm+=count
chk=[0 for _ in range(N)]
for i in range(N):
    if s[i][0]=="B" and s[i][-1]=="A":
        chk[i]=3
    elif s[i][-1]=="A":
        chk[i]=2
    elif s[i][0]=="B":
        chk[i]=1
x=0
y=0
z=0

ans=sm
for i in range(N):
    if chk[i]==3:
        z+=1
    if chk[i]==2:
        x+=1
    if chk[i]==1:
        y+=1
if z>0:
    a=z-1
    ans+=a
    if x>0:
        ans+=1
        x-=1
    if y>0:
        ans+=1
        y-=1
ans+=min(x,y)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
cnt_a = 0
cnt_b = 0
cnt_c = 0
cnt = 0
for i in range(n):
    s = input()
    for j in range(1, len(s)):
        if s[j - 1] == 'A' and s[j] == 'B':
            cnt += 1
    if s[0] == 'B' and s[-1] == 'A':
        cnt_c += 1
    elif s[0] == 'B':
        cnt_b += 1
    elif s[-1] == 'A':
        cnt_a += 1
ans = cnt
if cnt_c == 0:
    ans += min(cnt_a, cnt_b)
else:
    if cnt_a + cnt_b > 0:
        ans += cnt_c + min(cnt_a, cnt_b)
    else:
        ans += cnt_c - 1
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
import re
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10


def solve():
    n = II()
    s = SR(n)
    a = 0
    b = 0
    ab = 0
    ans = 0
    for si in s:
        if si[0] == "B" and si[-1] == "A":
            ab += 1
        elif si[0] == "B":
            b += 1
        elif si[-1] == "A":
            a += 1
        ans += si.count("AB")
    if ab:
        ans += ab - 1
        ans += (a > 0) + (b > 0)
        a -= a > 0
        b -= b > 0
    ans += min(a, b)
    print(ans)

    return



if __name__ == '__main__':
    solve()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


def main():
    ans = 0
    A = []
    B = []
    N = int(input())

    for _ in range(N):
        s = input()
        ans += s.count("AB")
        if s[0] == "B":
            B.append(1)
        else:
            B.append(0)
        if s[-1] == "A":
            A.append(1)
        else:
            A.append(0)
    if A == B:
        if sum(A) > 0:
            ans += sum(A) - 1
    else:
        ans += min(sum(A), sum(B))
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

Solution (Python):


N = int(input())
l = []
for i in range(N):
    l.append(input())





def count_inAB(s):
    counter = 0
    for i in range(len(s)-1):
        if(s[i] == 'A' and s[i+1] == 'B'):
            counter += 1
    return counter
'''
print(count_inAB('ABCA'))
print(count_inAB('ABCAB'))
print(count_inAB('ABABAB'))
print(count_inAB('ACBA'))
'''

lastA = []
for i in range(N):
    if(l[i][-1] == 'A'):
        lastA.append(i)



firstB = []
for i in range(N):
    if(l[i][0] == 'B'):
        firstB.append(i)







ans = 0
for i in range(N):
    ans += count_inAB(l[i])
    



connect = min(len(lastA), len(firstB))
if(lastA == firstB):
    connect -= 1
if(lastA == [] and firstB == []):
    connect += 1



ans += connect


print(ans)

Solution (Python):
n = int(input())
ret = 0
cnt_ab = 0
cnt_a = 0
cnt_b = 0

for i in range(n):
    s = input()
    ret += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        cnt_ab += 1
    elif s[0] == 'B':
        cnt_b += 1
    elif s[-1] == 'A':
        cnt_a += 1

if cnt_ab == 0:
    ret += min(cnt_a, cnt_b)
else:
    if cnt_a + cnt_b > 0:
        ret += cnt_ab + min(cnt_a, cnt_b)
    else:
        ret += (cnt_ab - 1)


print(ret)

Solution (Python):
n = int(input())
naibu = 0
head = 0
tail = 0
both = 0

for _ in range(n):
    s = str(input())
    naibu += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        head += 1
    elif s[-1] == 'A':
        tail += 1

ans = min(head, tail) + both + naibu
if head == 0 and tail == 0:
    ans = max(both-1, 0) + naibu
print(ans)

Solution (Python):
import re
n=int(input())
ans=0
a,b,ba=0,0,0
for i in range(n):
  s=input()
  ans+=len(re.sub("[A-Z]+","",s.replace("AB","s")))
  if s[0]=="B" and s[-1]=="A":ba+=1
  elif s[0]=="B":b+=1
  elif s[-1]=="A":a+=1
ans+=max(ba-1,0)
if ba:
  if a:ans+=1;a-=1
  if b:ans+=1;b-=1
print(ans+min(a,b))

Solution (Python):
N = int(input())

a, b, ba = 0, 0, 0
ret = 0
for _ in range(N):
    s = input()

    ret += s.count('AB')

    cond_a = s[-1] == 'A'
    cond_b = s[0] == 'B'
    if cond_a:
        if cond_b:
            ba += 1
        else:
            a += 1
    else:
        if cond_b:
            b += 1

if ba:
    ret += ba - 1
    if a:
        ret += 1
        a -= 1
    if b:
        ret += 1
        b -= 1
ret += min(a, b)

print(ret)

Solution (Python):
N = int(input())

from collections import defaultdict
d = defaultdict(int)
cnt = 0
for i in range(N):
  s = input().replace("AB","
  cnt += s.count("
  l = 1 if s[0]  == "B" else 0
  r = 1 if s[-1] == "A" else 0
  d[(l,r)] += 1

if d[(1,0)]>0  and d[(0,1)]>1 :
  print(cnt+d[(1,1)] + min(d[(0,1)],d[(1,0)]))
elif d[(1,0)] >0 or d[(0,1)]>0:
  print(cnt+d[(1,1)] + min(d[(0,1)],d[(1,0)]))
else:
  print(cnt+max(0,d[(1,1)]-1))

Solution (Python):
N = int(input())

count_ab = 0
count_start_b = 0
count_finish_a = 0
count_start_b_finish_a = 0
for i in range(N):
    s = input()
    count_ab += s.count('AB')
    
    if s[0] == 'B' and s[-1] == 'A':
        count_start_b_finish_a += 1
    elif s[0] == 'B':
        count_start_b += 1
    elif s[-1] == 'A':
        count_finish_a += 1



if not count_start_b_finish_a:
    print(count_ab + min(count_finish_a, count_start_b))
elif max(count_finish_a, count_start_b) > 0 and count_start_b_finish_a:
    print(count_ab + count_start_b_finish_a + min(count_finish_a, count_start_b))
elif count_start_b_finish_a:
    print(count_ab + count_start_b_finish_a - 1)

Solution (Python):
n = int(input())

ans = 0

BA = 0
XA = 0
BX = 0

for i in range(n):
    s = input()
    ans += s.count('AB')

    if s[0] == 'B' and s[-1] == 'A':
        BA += 1
    elif s[-1] == 'A':
        XA += 1
    elif s[0] == 'B':
        BX += 1

if BA > 0:
    ans += BA-1
    BA = 1
    if BX > 0:
        ans += 1
        BA = 0
    else:
        BX = 1
        BA = 0

ans += min(XA, BX)

print(ans)

Solution (Python):
import sys

n, *s = sys.stdin.readlines()
a = 0
b = 0
ba = 0
ans = 0
for i in s:
    if i[0] == 'B' and i[-2] == 'A':
        ba += 1
    elif i[0] == 'B':
        b += 1
    elif i[-2] == 'A':
        a += 1
    ans += i.count('AB')
if a == 0 and b == 0 and ba > 0:
    ba -= 1
ans += ba + min(a, b)
print(ans)

Solution (Python):
N = int(input())
A = 0
B = 0
AB = 0
ans = 0
for _ in range(N):
    s = list(input())
    if s[0] == "B" and s[-1] == "A":
        AB += 1
    elif s[0] == "B":
        B += 1
    elif s[-1] == "A":
        A += 1
    for i in range(len(s) - 1):
        if s[i:i + 2] == list("AB"):
            ans += 1
if A == 0 and B == 0 and AB > 0:
    ans -= 1
print(ans + AB + min(A, B))

Solution (Python):

N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s.startswith('B') and s.endswith('A'):
        a += 1
    elif s.startswith('B'):
        b += 1
    elif s.endswith('A'):
        c += 1

    d += s.count('AB')



if a == 0:
    print(d + min(b, c))

elif b == 0 and c == 0:
    print(d + a -1 )

else:
    print(d + a + min(b, c))

Solution (Python):
n = int(input())
count = 0

a, b, ab = 0, 0, 0
for _ in range(n):
    s = input()
    if s[-1] == "A" and s[0] == "B":
        ab += 1
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1


    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            count += 1

count += min(a, b)
if max(a, b) == ab and ab != 0:
    count -= 1

print(count)

Solution (Python):
def main():
    words = int(input())
    first_b = [""]
    last_a = [""]
    others = []
    ab = []
    for _ in range(words):
        s = input()
        if s[0] == "B" and s[-1] == "A":
            ab.append(s)
        elif s[-1] == "A":
            last_a.append(s)
        elif s[0] == "B":
            first_b.append(s)
        else:
            others.append(s)
    connect = []
    for i in range(max(len(last_a), len(first_b))):
        if i < len(last_a) - 1:
            connect.append(last_a[i])
        if i < len(first_b) - 1:
            connect.append(first_b[i])
    word = last_a[-1] + "".join(ab) + first_b[-1] + "".join(connect) + "".join(others)
    print(word.count("AB"))


if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
s = [input() for _ in range(n)]
tail_a = 0
head_b = 0
both_have = 0
ans = 0
for i in range(n):
    if s[i][0] == "B": head_b += 1
    if s[i][-1] =="A": tail_a += 1
    if s[i][0] == "B" and s[i][-1] =="A":
        both_have += 1
    ans += s[i].count("AB")
ans += min(tail_a,head_b)
if tail_a == head_b and tail_a == both_have:
    if both_have > 0:
        ans -= 1
print(ans)

Solution (Python):
import bisect




import itertools



import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    b_count = 0
    a_count = 0
    b_and_a_count = 0
    ans = 0

    for i in range(n):
        s = sys.stdin.readline().rstrip("\n")
        len_s = len(s)
        for j in range(len_s-1):
            if s[j] == "A":
                if s[j+1] == "B":
                    ans += 1
        if s[0] == "B" and s[-1] == "A":
            b_and_a_count += 1
        elif s[0] == "B":
            b_count += 1
        elif s[-1] == "A":
            a_count += 1

    if b_and_a_count > 0:
        ans += (b_and_a_count-1)
        if a_count > 0:
            ans += 1
            a_count -= 1
        if b_count > 0:
            ans += 1
            b_count -= 1

    ans += min(a_count, b_count)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
numA = 0
numB = 0
cnt = 0
sm = 0

for i in range(N):
    mm = input()
    if mm[0]=="B":
        numB += 1
    if mm[-1]=="A":
        numA += 1
    if mm[0]=="B" and mm[-1]=="A":
        sm += 1

    for i in range(len(mm)):
        if mm[i:i+2]=="AB":
            cnt += 1

if sm == numA and sm==numB and sm>0:
    numA -= 1
    numB -= 1
mm2 = min(numA,numB,N-1)
print(cnt+mm2)

Solution (Python):
n=int(input())
ans=0
a=0
b=0
flg=1
for _ in range(n):
  s=input()
  for i in range(len(s)-1):
    if s[i:i+2]=="AB":
      ans+=1
  if s[0]=="B":
    b+=1
    if s[-1]!="A":
      flg=0
  if s[-1]=="A":
    a+=1
    if s[0]!="B":
      flg=0
if flg:
  ans+=max(0,min(a,b)-1)
else:
  ans+=min(a,b)
print(ans)

Solution (Python):
N = int(input())

S = []

for _ in range(N):
    S.append(input())



internal_ab = 0
for s in S:
    for i in range(1,len(s)):
        if s[i-1] == 'A' and s[i] == 'B':
            internal_ab += 1


a_pool = 0
b_pool = 0
ab_pool = 0
edge_ab = 0
for s in S:
    if s[0] == 'B' and s[-1] == 'A':
        ab_pool += 1 
    elif s[0] == 'B':
        b_pool += 1
    elif s[-1] == 'A':
        a_pool += 1

if ab_pool == 0:
    edge_ab = min(a_pool, b_pool)
elif a_pool + b_pool == 0:
    edge_ab = ab_pool - 1
else:
    edge_ab = ab_pool + min(a_pool, b_pool)
    
print(internal_ab+edge_ab)

Solution (Python):
N=int(input())
S=[input() for i in range(N)]
BXA=0;XA=0;BX=0;ans=0;
for s in S:
  ans+=s.count("AB")
  if(s[0]=='B' and s[-1]=='A'):
    BXA+=1
  elif(s[0]=='B'):
    BX+=1
  elif(s[-1]=='A'):
    XA+=1

if min([XA,BX])>0:
  ans+=min([XA,BX])+BXA
elif max([XA,BX])>0:
  ans+=BXA
else:
  ans+=max([BXA-1,0])
print(ans)

Solution (Python):
N=int(input())
S=[input() for i in range(N)]
BA=0
B=0
A=0
cnt=0
for i in S:
  if i[0]=='B'and i[-1]=='A':
    BA+=1
  elif i[0]=='B':
    B+=1
  elif i[-1]=='A':
    A+=1
  for j in range(len(i)-1):
    if i[j]=='A' and i[j+1]=='B':
      cnt+=1
if BA==0:
  print(cnt+min(A,B))
else:
  ans=BA-1
  if B>0:
    ans+=1
    B-=1
  if A>0:
    ans+=1
    A-=1
  print(cnt+min(A,B)+ans)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
count,a,b,ab = 0,0,0,0
for word in s:
  count += word.count("AB")
  if word[0] == "B" and word[-1] =="A":
    ab += 1
  elif word[0] == "B":
    b += 1
  elif word[-1] == "A":
    a += 1
  else:
    pass
if ab == 0:
  print(min([a,b])+count)
else:
  if a == 0 and b == 0:
    print(ab-1+count)
  elif a == 0 or b == 0:
    print(ab+count)
  else:
    print(ab+1+min([a-1,b-1])+count)

Solution (Python):
n = int(input())
a = b = ab = ans = 0
for i in range(n):
        s = input()
        if s[-1]=='A':
                if s[0]=='B': ab += 1
                else: a += 1
        elif s[0]=='B': b += 1
        ans += s.count("AB")

ans += max(0, ab - 1)
if a>0 and ab>0:
        ans += 1
        a -= 1
if b>0 and ab>0:
        ans += 1
        b -= 1
ans += min(a, b)
print(ans)

Solution (Python):
N = int(input())
cntAB = 0
cntA = 0
cntB = 0
BcntA = 0
for i in range(N):
  s = input()
  if s[0] == "B" and s[-1] == "A":
    BcntA += 1
  elif s[0] == "B" and s[-1] != "A":
    cntB += 1
  elif s[0] != "B" and s[-1] == "A":
    cntA += 1
  for j in range(1,len(s)):
    if s[j-1] == "A" and s[j] == "B":
      cntAB += 1
if BcntA!=0:
  if cntA+cntB>0:
    print(cntAB+min(cntA,cntB)+BcntA)
  else:
    print(cntAB+BcntA-1)
else:  
  print(cntAB+min(cntA,cntB))

Solution (Python):
N = int(input())
ans = 0
da =0
db = 0
dba = 0
for i in range(N):
    s = input()
    k = s.count('AB')
    ans += k
    if s[0] == 'B' and s[-1] == 'A':
        dba +=1
        continue
    if s[0] == 'B':
        db +=1
    if s[-1] == 'A':
        da +=1
if dba != 0:
    ans += dba -1
    if da >= 1:
        ans +=1
        da -=1
    if db >=1:
        ans +=1
        db -=1
    ans += min(da,db)
    print(ans)
    exit()
if dba == 0:
    ans += min(da,db)
    print(ans)
    exit()

Solution (Python):
n=int(input())
S=[input() for _ in range(n)]

ans,acnt,bcnt,abcnt=0,0,0,0
for s in S:
  ans+=s.count('AB')
  if s[-1]=='A' and s[0]=='B': abcnt+=1
  elif s[-1]=='A': acnt+=1
  elif s[0]=='B': bcnt+=1
if abcnt:
  ans+=abcnt-1
  if acnt:
    ans+=1
    acnt-=1
  if bcnt:
    ans+=1
    bcnt-=1
ans+=min(acnt,bcnt)
print(ans)

Solution (Python):
import sys


def main():
    N = int(input())
    AB = [0,0,0]
    ans = 0
    for _ in range(N):
        s = input()
        l = len(s)
        if s[0] == "B" and s[-1] == "A":
            AB[2] += 1
        else:
            if s[0] == "B":
                AB[1] += 1
            if s[-1] == "A":
                AB[0] += 1
        for i in range(l-1):
            if s[i] == "A" and s[i+1] == "B":
                ans += 1

    if (AB[0]+AB[1] == 0 and AB[2] != 0):
        print(ans+AB[2]-1)
    else:
        print(ans+min(AB[0],AB[1])+AB[2])
    
if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
S=[input() for _ in range(N)]
AB=sum(s.count("AB") for s in S)
EA=sum(s.endswith("A") for s in S)
SB=sum(s.startswith("B") for s in S)
EASB=sum(s.endswith("A") and s.startswith("B") for s in S)
K=min(EA,SB)-(EA==SB==EASB!=0)
print(AB+K)

Solution (Python):
N = int(input())

a, b, c, d = 0, 0, 0, 0
for _ in range(N):
    s = input()
    if s[0]=="B" and s[-1]=="A":
        a += 1
    elif s[0]=="B":
        b += 1
    elif s[-1]=="A":
        c += 1
    d += s.count("AB")

ans = d
if b==c==0:
    ans += max(0, a-1)
else:
    ans += a + min(b, c)
print(ans)

Solution (Python):
n=int(input())
s=[input() for i in range(n)]
count=0
A=0
B=0
BA=0
for i in range(n):
    for j in range(len(s[i])-1):
        if j==0:
            if s[i][j]=='B':
                B+=1
        if j==len(s[i])-2:
            if s[i][j+1]=='A':
                A+=1
        if s[i][j:j+2]=='AB':
            count+=1
    if s[i][0]=='B' and s[i][-1]=='A':
        BA+=1
        A-=1
        B-=1

if BA==0:
    print(count+min(A,B))
else:
    ans=count+BA-1
    if A>=1 and B>=1:
        print(ans+2+min(A,B)-1)
    elif A>=1 or B>=1:
        print(ans+1)
    else:
        print(ans)

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]

cnt_AB = 0
cnt_last_A = 0
cnt_start_B = 0
cnt_start_B_last_A = 0

for i in range(n):
    cnt_AB += s[i].count('AB')
    st = s[i][0]
    la = s[i][-1]
    if st == 'B' and la == 'A':
        cnt_start_B_last_A += 1
    elif st == 'B':
        cnt_start_B += 1
    elif la == 'A':
        cnt_last_A += 1

if cnt_start_B_last_A == 0:
    print(cnt_AB + min(cnt_start_B, cnt_last_A))
else:
    if cnt_start_B + cnt_last_A > 0:
        print(cnt_AB + cnt_start_B_last_A + min(cnt_start_B, cnt_last_A))
    else:
        print(cnt_AB + cnt_start_B_last_A - 1)

Solution (Python):
N=int(input())
a,b,ba=0,0,0
ret = 0
for i in range(N):
  st = input()
  ret+=st.count('AB')
  if st[0]=='B' and st[-1]=='A':
    ba+=1
  elif st[0]=='B':
    b+=1
  elif st[-1]=='A':
    a+=1
if ba>0:
  ret+=ba-1
  if a>0 and b>0:
    ret+=2
    ret+=min(a-1,b-1)
  elif a==0 and b==0:
    ret+=0
  else:
    ret+=1
else:
  ret+=min(a,b)
print(ret)

Solution (Python):
n=int(input())
ans=0
a=0;b=0;ab=0
for i in range(n):
  s=input()
  ans+=s.count("AB")
  if s[0]=="B":
    b+=1
    if s[-1]=="A":
      ab+=1
      b-=1
  elif s[-1]=="A":
    a+=1
if a+b==0:
    print(ans+max(ab-1,0))
else:
    print(ans+min(a,b)+ab)

Solution (Python):
n=int(input())
a=0
b=0
c=0
d=0
for i in range(n):
  S=input()
  for i in range(len(S)-1):
    if S[i]=="A" and S[i+1]=="B":
      c=c+1
  if S[0]=="B":
    b=b+1
  if S[-1]=="A":
    a=a+1
  if S[0]=="B" and S[-1]=="A":
    d=d+1
if a==b and b==d and a!=0:
  print(c+min(a,b)-1)
else:
  print(c+min(a,b))

Solution (Python):
n = int(input())
ab,xa,bx,bxa = 0,0,0,0
for i in range(n):
  s = input()
  if s[-1] == 'A' and s[0] == 'B':
    bxa += 1
  if s[-1] == 'A':
    xa += 1
  if s[0] == 'B':
    bx += 1
  for j in range(len(s)-1):
    if s[j]+s[j+1] == 'AB':
      ab += 1
if bxa == xa == bx:
  print(ab+bxa-bool(bxa))
else:
  print(ab+min(xa,bx))

Solution (Python):
n=int(input())
a=0
b=0
ab=0
x=0
for i in range(n):
  s=input()
  l=len(s)-1
  if s[0]=='B':
    if s[l]=='A':
      ab+=1
    else:
      b+=1
  elif s[l]=='A':
    a+=1
  x+=s.count('AB')
if ab>0:
  x+=ab-1
  x+=(a>0)+(b>0)
  a-=1;b-=1
a=max(0,a);b=max(0,b)
x+=min(a,b)
print(x)