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())
stlist = []
bf = 0
al = 0
ab = 0
ba = 0

for i in range(N):
    tmp = str(input())
    ab += tmp.count("AB")
    if tmp[0] == 'B' and tmp[-1] == 'A':
        ba += 1
    elif tmp[0] =='B':
        bf+=1
    elif tmp[-1] =='A':
        al+=1
if al+bf+ba==0:
    print(ab)
elif al == 0 and bf ==0:
    print(ba-1+ab)
elif al * bf ==0:
    print(max(min(al-1, bf-1), 0) + ba + ab)
else:
    print(max(min(al-1, bf-1), 0) + ba +1 + ab)

Solution (Python):
n = int(input())
ss = []
for i in range(n):
    ss.append(input())


a = 0
b = 0
c = 0
d = 0
for s in ss:

    d += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        c += 1
    elif s[0] == "B":
        b += 1
    elif s[-1] == "A":
        a += 1


if c > 0:
    d+= c-1
    if a > 0 :
        d+=1
        a -= 1
    if b > 0 :
        d+=1
        b -= 1

print(d + min(a,b))

Solution (Python):
n = int(input())
firsts = 0
lasts = 0
doubles = 0
inners = 0
for i in range(n):
    s = input()
    first = 1 if (s[0] == "B" and s[-1] != "A") else 0
    last = 1 if (s[-1] == "A" and s[0] != "B") else 0
    double = 1 if (s[0] == "B" and s[-1] == "A") else 0
    inner = s.count("AB")
    
    firsts += first
    lasts += last
    doubles += double
    inners += inner
if doubles == 0 and firsts == 0 and lasts == 0:
    print(inners)
elif firsts == 0 and lasts == 0:
    print(inners + doubles - 1)


else:
    print(inners + doubles + min(firsts, lasts))

Solution (Python):
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)

if both>=1:
    ans+=both-1
    if a_last>=1:
        ans+=1
        a_last-=1
    if b_start>=1:
        ans+=1
        b_start-=1
ans+=min(a_last,b_start)
print(ans)

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

cnt = 0
a_flag = 0
b_flag = 0
ab_flag = 0

s = [0]*n
for i in range(n):
    s[i] = input()

    for j in range(len(s[i])-1):
        if s[i][j:j+2] == 'AB':
            cnt += 1

    if s[i][0] == 'B' and s[i][-1] != 'A':
        b_flag += 1
    elif s[i][-1] == 'A' and s[i][0] != 'B':
        a_flag += 1
    elif s[i][0] == 'B' and s[i][-1] == 'A':
        ab_flag += 1

if ab_flag != 0:
    if a_flag == 0 and b_flag == 0:
        print(cnt + ab_flag - 1)
    elif a_flag>=1 and b_flag >= 1:
        print(cnt + ab_flag + 1 + min(a_flag - 1, b_flag - 1))
    else:
        print(cnt + ab_flag) 
else:
    print(cnt + min(a_flag, b_flag))

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

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

counter = 0
begin_b = []
end_a = []
begin_b_and_end_a = []
for i in range(n):
    counter += s[i].count('AB')
    if s[i][0] == 'B':
        begin_b.append(i)
    if s[i][-1] == 'A':
        end_a.append(i)
    if s[i][0] == 'B' and s[i][-1] == 'A':
        begin_b_and_end_a.append(i)

if len(begin_b) == len(begin_b_and_end_a) and len(begin_b_and_end_a) == len(end_a) and len(end_a) > 0:
    print(counter + min(len(begin_b), len(end_a)) - 1)
else:
    print(counter + min(len(begin_b), len(end_a)))

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

a_end = 0
b_start = 0
ab = 0
zz = 0
for n in range(N):
    s = input()
    ab = ab + s.count('AB')
    if s[-1] == 'A':
        a_end = a_end + 1
    if s[0] == 'B':
        b_start = b_start + 1
    if s[-1] != 'A' and s[0] != 'B':
        zz = zz + 1

print(ab + max(min(a_end, b_start, N - 1 - zz), 0))

Solution (Python):
n = int(input())
ab = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ab = ab + s.count('AB')
    start_b = s[:1] == 'B'
    end_a = s[-1:] == 'A'
    if start_b:
        b = b + 1
    if end_a:
        a = a + 1
    if start_b and end_a:
        ba = ba + 1

b = b - ba
a = a - ba

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

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

strings = []
for _ in range(N):
    strings.append(input())

cnt = 0
cnt_A = cnt_B = 0
flag = False
for s in strings:
    for c1, c2 in zip(s, s[1:]):
        if c1 == 'A' and c2 == 'B':
            cnt += 1

    if s[0] == 'B':
        cnt_B += 1
        if s[-1] != 'A':
            flag = True
    if s[-1] == 'A':
        cnt_A += 1
        if s[0] != 'B':
            flag = True

cnt += min(cnt_A, cnt_B)
if cnt_A == 0 and cnt_B == 0:
    flag = True
print(cnt if flag else cnt - 1)

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

Solution (Python):

n = int(input())
s = list()
counter = {
    'ab': 0,
    'begin_b': 0,
    'end_a': 0,
    'b_to_a': 0
}

for i in range(n):
    substr = input()
    counter['ab'] += substr.count('AB')
    if substr.startswith('B'):
        if substr.endswith('A'):
            counter['b_to_a'] += 1
        else:
            counter['begin_b'] += 1
    else:
        if substr.endswith('A'):
            counter['end_a'] += 1

total = counter['ab'] + min(counter['begin_b'], counter['end_a'])
if counter['b_to_a'] > 0:
    total += (counter['b_to_a'] - 1)
    if max(counter['begin_b'], counter['end_a']) > 0:
        total += 1

print(total)

Solution (Python):
n = int(input())
l = [input() for i in range(n)]
a = 0
b = 0
ba = 0
ab = 0
for i in range(n):
    if 'AB' in l[i]:
        for j in range(len(l[i])-1):
            if l[i][j] == 'A' and l[i][j+1] == 'B':
                ab += 1
    if l[i][0] =='B' and l[i][-1] == 'A':
        ba += 1
    elif l[i][-1] == 'A':
        a += 1
    elif l[i][0] == 'B':
        b += 1
if max(a,b) == 0 and ba <= 1:
    print(ab)
elif max(a,b) == 0:
    print(ab+ba-1)
else:
    print(min(a,b)+ab+ba)

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

cnt = 0
cntA = 0
cntB = 0
cntAB = 0

for s in S :
    for i in range(1, len(s)) :
        if s[i - 1] =='A' and s[i] == 'B':
            cnt += 1
    if s[0] == 'B' and s[len(s) - 1] == 'A':
        cntAB += 1
    elif s[0] == 'B' :
        cntB += 1
    elif s[len(s) - 1] == 'A' :
        cntA += 1

ans = min(cntA, cntB)
if cntAB >= 2 :
    ans += (cntAB - 1)
if cntAB >= 1 and max(cntA, cntB) != 0:
    ans += 1

'''
if max(cntA, cntB) == 0:
    ans = cntAB - 1
else :
    ans += cntAB
'''
if ans == len(S) :
    ans -= 1

print(cnt + ans)

Solution (Python):
def solve():
    N = int(input())
    S = [input() for i in range(N)]
    ans = sum([s.count('AB') for s in S])
    a1 = len([1 for s in S if s[-1] == 'A'])
    b1 = len([1 for s in S if s[0] == 'B'])
    ab1 = len([1 for s in S if s[-1] == 'A' and s[0] == 'B'])

    
    if a1 > b1:
        return ans + b1
    elif a1 < b1:
        return ans + a1

    if a1 == ab1 and ab1 > 0:
        return ans + ab1 - 1

    return ans + a1

print(solve())

Solution (Python):
import re
n = int(input())
s = []
compiled = re.compile(r"AB")

pre = 0
xa=0
bx=0
ba=0
for i in range(n):
    one = input()
    pre+=len(compiled.findall(one))
    f=one[0]
    l=one[-1]


    if f=="B" and l=="A":
        ba+=1
    elif f=="B" and l!="A":
        bx+=1
    elif f!="B" and l=="A":
        xa+=1


cnt=0
if ba>=1:
    cnt+=ba-1
    if xa>=1:
        xa-=1
        cnt+=1
    
    if bx>=1:
        bx-=1
        cnt+=1

cnt += xa if xa<bx else bx
print(cnt+pre)

Solution (Python):

n = int(input())
s = []

for i in range(n):
    s.append(input())

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

if ba != 0:
    ab += ba - 1
    if a > b:
        b += 1
    elif b > a:
        a += 1
    elif a == b and a >= 1:
        a += 1
        b += 1

print(ab + min(a,b))

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

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

ab_count = 0
bottom_a = 0
top_b = 0
ba = 0
for s in S:
    if s[-1] == 'A' and s[0] == 'B':
        ba += 1
    else:
        if s[-1] == 'A':
            bottom_a += 1
        if s[0] == 'B':
            top_b += 1
    for i in range(1,len(s)):
        if s[i-1] == 'A' and s[i] == 'B':
            ab_count += 1
if ba > 0:
    if bottom_a > 0:
        bottom_a -= 1
        ab_count += 1
    if top_b > 0:
        top_b -=  1
        ab_count += 1
ans = max(ba - 1, 0) + min(bottom_a, top_b) + ab_count
print(ans)

Solution (Python):
N=int(input())
ls=[input() for i in range(N)]
org_num=0
XA_num=0
BA_num=0
BX_num=0

for i in ls:
    org_num+=i.count('AB')
    listed_word=list(i)
    if listed_word[0]=='B' and listed_word[-1]=='A':
        BA_num+=1
    elif listed_word[0]=='B':
        BX_num+=1
    elif listed_word[-1]=='A':
        XA_num+=1



if BX_num:
    BX_num_addition=1
else:
    BX_num_addition=0

if XA_num:
    answer=org_num+BA_num+BX_num_addition
elif BA_num:
    answer=org_num+BA_num+BX_num_addition-1
else:
    answer=org_num

XA_num-=1
BX_num-=1

shiage=min(XA_num,BX_num)
if shiage<0:
    shiage=0

answer+=shiage
print(answer)

Solution (Python):

N = int(input())

naka=0
a_kazu=0
b_kazu=0
b_a_kazu=0
for i in range(N):
    a = str(input())
    naka+=a.count("AB")
    if (a[0]=="B" and a[-1]=="A"):
        b_a_kazu+=1
    elif a[0]=="B":
        b_kazu+=1
    elif a[-1]=="A":
        a_kazu+=1
if b_a_kazu==0:
    print(naka+min(a_kazu, b_kazu))
else:
    if (a_kazu==0 and b_kazu==0):
        print(naka+(b_a_kazu-1))
    elif (a_kazu==0 or b_kazu==0):
        print(naka+(b_a_kazu))
    else:
        print(naka+(b_a_kazu-1)+min(a_kazu+1, b_kazu+1))

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

naiv=0
endsA=0
startsB=0
both=0

for i in range(N):
    s=input()
    naiv+=s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        both += 1
    elif s[0] == 'B':
        startsB += 1
    elif s[-1] == 'A':
        endsA += 1






res=naiv
if both >= startsB and both >= endsA and (endsA > 0 and startsB > 0):
    lower = min(endsA, startsB)
    higher = max(endsA, startsB)
    res += lower*2
    both -= lower
    startsB -= lower
    endsA -= lower
    res += both
    both = 0
elif both <= startsB and both <= endsA:
    res += both*2
    endsA -= both
    startsB -= both
    both = 0
    res += min(endsA, startsB)

elif both > 0 and max(endsA, startsB) > 0:
    lower = min(endsA, startsB)
    res += 2*lower
    both -= lower
    endsA -= lower
    startsB -= lower
    res += both
elif both > 0 and max(endsA, startsB) == 0:
    res += both-1
else:
    res += min(endsA, startsB)

print(res)

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

top = [0 for _ in range(N)]
last = [0 for _ in range(N)]

res = 0
n = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            res += 1
            
    if s[0] == 'B':
        top[n] = 1
        
    if s[-1] == 'A':
        last[n] = 1
        
    n += 1
 
d = 0
t = 0
l = 0
for i in range(N):
    if top[i] == 1 and last[i] == 1:
        d += 1
    elif top[i] == 1:
        t += 1
    elif last[i] == 1:
        l += 1
     

if d >= 1:
    res += d - 1



if d > 0 and min(t, l) >= 1:
    res += min(t-1, l-1)
    res += 2
    
elif d > 0 and min(t, l) < 1 and max(t, l) >= 1:
    res += 1
    
else:
    res += min(t, l)
    
    
print(res)

Solution (Python):
n = int(input())
inab = 0
topb = 0
taila = 0
topb_taila = 0
for _ in range(n):
    s = str(input())
    if (s[0] == "B")&(s[-1] == "A"):
        topb_taila += 1
    else:
        if (s[0] == "B"):
            topb += 1
        if (s[-1] == "A"):
            taila += 1
    
    bef =""
    for i in range(len(s)):
        if (bef=="A")&(s[i]=="B"):
            inab += 1
        bef = s[i]

ans = inab

topb += topb_taila
taila += topb_taila

if topb==taila:
    if topb==topb_taila:
        if topb==0:
            ans+=0
        else:
            ans += topb-1
    else:
        ans += topb
else:
    ans += min(topb,taila)

print(ans)

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

a = 0
b = 0
b_a = 0
ab = 0
for j in range(N):
    if s[j][0] == 'B':
        b += 1
    if s[j][-1] == 'A':
        a += 1
    
    if s[j][0] == 'B' and s[j][-1] == 'A':
        b_a += 1

    ab += s[j].count('AB')

if b_a >= 1 and b_a == a and b_a == b:
    print(ab + a - 1)
else:
    print(ab + min(a,b))

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

count = 0
count_BA = 0
count_A = 0
count_B = 0
for i in range(N):
    count += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        count_BA += 1
    elif s[i][0] == 'B':
        count_B += 1
    elif s[i][-1] == 'A':
        count_A += 1

if count_B != 0 and count_A != 0:
    print(count + count_BA - 1 + 1 + 1 + min(count_A, count_B) - 1)
elif count_B != 0 or count_A != 0:
    print(count + count_BA - 1 + 1)
elif count_BA != 0:
    print(count + count_BA - 1)
else:
    print(count)

Solution (Python):
N = int(input())
a, b, ab, ret = 0, 0, 0, 0
for i in range(N):
    s = input()
    l = len(s) - 1
    if s[0] == "B" and s[l] == "A":
        ab += 1
    if s[0] == "B":
        b += 1
    if s[l] == "A":
        a += 1
    ret += s.count("AB")
if a == b and a == ab and a > 0:
    a -= 1
ab = min(a,b)
print(ret + ab)

Solution (Python):
def ab_substrings(N: int, S: list)->int:
    res = 0
    endA = 0
    startB = 0
    both = 0

    for s in S:
        res += s.count('AB')
        if s.endswith('A'):
            if s.startswith('B'):
                both += 1
            else:
                endA += 1
        elif s.startswith('B'):
            startB += 1

    if both > 0:
        res += both - 1

        if endA > 0:
            res += 1
            endA -= 1

        if startB > 0:
            res += 1
            startB -= 1

    return res + min(endA, startB)


if __name__ == "__main__":
    N = int(input())
    S = [input() for _ in range(N)]

    ans = ab_substrings(N, S)
    print(ans)

Solution (Python):
N = int(input())
a, b, ab, ret = 0, 0, 0, 0
for i in range(N):
    s = input()
    l = len(s) - 1
    if s[0] == "B" and s[l] == "A":
        ab += 1
    if s[0] == "B":
        b += 1
    if s[l] == "A":
        a += 1
    ret += s.count("AB")
if a == b and a == ab and a > 0:
    a -= 1
ab = min(a,b)
print(ret + ab)

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

cnt = 0
cntA = 0
cntB = 0
cntAB = 0

for s in S :
    for i in range(1, len(s)) :
        if s[i - 1] =='A' and s[i] == 'B':
            cnt += 1
    if s[0] == 'B' and s[len(s) - 1] == 'A':
        cntAB += 1
    elif s[0] == 'B' :
        cntB += 1
    elif s[len(s) - 1] == 'A' :
        cntA += 1

ans = min(cntA, cntB)
if cntAB >= 2 :
    ans += (cntAB - 1)
if cntAB >= 1 and max(cntA, cntB) != 0:
    ans += 1

'''
if max(cntA, cntB) == 0:
    ans = cntAB - 1
else :
    ans += cntAB
'''
if ans == len(S) :
    ans -= 1

print(cnt + ans)

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

ab_num = 0

"""
b_start = 0
a_end = 0
for i in range(N):
    s = S[i]
    for j in range(1, len(s)):
        if s[j] == 'B' and s[j-1] == 'A':
            ab_num += 1
    if s[0] == 'B':
        b_start += 1
    if s[len(s)-1] == 'A':
        a_end += 1

print(ab_num + min(a_end, b_start))


BABA
ABABA
BAABBAAB
BBAAAAB
ABABABAB
"""

x_A = 0
B_A = 0
B_x = 0


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

    if s[0] == 'B':
        if s[len(s) - 1] == 'A':
            B_A += 1
        else:
            B_x += 1
    elif s[len(s) - 1] == 'A':
        x_A += 1

if B_A > 0:
    if x_A * B_x > 0:
        print(ab_num + B_A-1 + 2 + min(x_A-1, B_x-1))
    else:
        if x_A + B_x > 0:
            print(ab_num + B_A)
        else:
            print(ab_num + B_A-1)
else:
    print(ab_num + min(x_A, B_x))

Solution (Python):
N=int(input())
s=[input() for _ in range(N)]
def checkAB(si):
  cnt=0
  for i in range(len(si)-1):
    if si[i]=='A' and si[i+1]=='B':cnt+=1
  return cnt

ABin=sum([checkAB(si) for si in s])
Aend=sum([si[len(si)-1]=='A' for si in s])
Bstart=sum([si[0]=='B' for si in s])
BstrA=sum([si[0]=='B' and si[len(si)-1]=='A' for si in s])

if BstrA==Aend and BstrA==Bstart and BstrA>0:
  ans=ABin+BstrA-1
else:
  ans=ABin+min(N-1,Aend,Bstart)
print(ans)

Solution (Python):

N =  int(input())
S = [input() for _ in range(N)]

cnt = 0
b, a = 0, 0
ab = 0
for s in S:
  cnt += s.count("AB")
  if s[0] == "B" and s[-1] == "A":
    ab += 1
  elif s[0] == "B":
    b += 1
  elif s[-1] == "A":
    a += 1
if ab > 1:
  cnt += ab-1
  ab = 1
if ab:
  if a:
    cnt += 1
    a -= 1
  if b:
    cnt += 1
    b -= 1
at = 0
bt = 0
while a > 0 and b > 0:
  if a:
    at += 1
    a -= 1
  if b:
    bt += 1
    b -= 1
  if at + bt == 2:
    cnt += 1
  at = 0
  bt = 0
print(cnt)

Solution (Python):
N = int(input())
slist = [input() for _ in range(N)]
cnt = 0
bnum = 0
anum = 0
banum = 0
for s in slist:
    cnt+= s.count('AB')
    if s[0]=='B':
        bnum+=1
        if s[-1]=='A':
            banum+=1
    if s[-1]=='A':
        anum+=1
if banum == anum == bnum:
    if banum==0:
        print(cnt)
    else:
        print(cnt+banum-1)
else:
    print(cnt+min(bnum,anum))

Solution (Python):
N = int(input())
S = [input() for i in range(N)]
count = 0
con = []
Alis = []
Blis = []
for s in S:
    count += s.count('AB')
for i in range(N):
    if S[i][-1:] == 'A' and S[i][0] == 'B':
        con.append(S[i])
    elif S[i][-1:] == 'A':
        Alis.append(S[i])
    elif S[i][0] == 'B':
        Blis.append(S[i])
if len(Alis)== 0 and len(Blis) == 0 and len(con) > 0:
    count += len(con)-1
else:
    count += min(len(con)+len(Alis),len(con)+len(Blis))
print(count)

Solution (Python):


N = int(input())
ls =[]
for _ in range(N):
    ls.append(input())

count = 0
for i in  ls:
    count += i.count('AB')

countB =0
countA = 0
countBA = 0
for i in ls:
    if i[0] == 'B' and i[-1] == 'A':
        countBA += 1
    elif i[0] == 'B':
        countB += 1
    elif i[-1] == 'A':
        countA += 1

if countBA ==0:
    count += min(countA,countB)
elif countA ==0 and countB == 0:
    count += countBA-1
elif countA == 0 or countB ==0:
    count += countBA
else:
    count += countBA + min (countA,countB)


print(count)

Solution (Python):







n=int(input())
S=[]
ans=0
firstB,lastA,bothAB=0,0,0

for i in range(n):
    S.append(input())
    flag=0
    for j in range(len(S[-1])-1):
        if S[-1][j:j+2]=="AB":
            ans+=1
    if S[-1][0]=="B":
        firstB+=1
        flag+=1
    if S[-1][-1]=="A":
        lastA+=1
        flag+=1
    if flag==2:
        bothAB+=1
        firstB-=1
        lastA-=1

if firstB==lastA:
    if firstB==0 and bothAB!=0:
        ans+=bothAB-1
    else:
        ans+=(bothAB+firstB)
else:
    ans+=min(firstB,lastA)+bothAB

print(ans)

Solution (Python):
N = int(input())
s_ = [input() for _ in range(N)]

center = 0
s_aend = []
s_bstart = []
s_bsae = []
s_else = []
center = 0

for s in s_:
  if s.startswith("B") and s.endswith("A"):
    s_bsae.append(s)
  elif s.startswith("B"):
    s_bstart.append(s)
  elif s.endswith("A"):
    s_aend.append(s)
  else:
    center += s.count("AB")

s_max = ""
if len(s_bsae) > 0:
  s_max = ''.join(s_bsae)

if len(s_bstart) > 0 and len(s_bsae) > 0:
  center += 1
  b_len = len(s_bstart)-1
else:
  b_len = len(s_bstart)
  
if len(s_aend) > 0 and len(s_bsae) > 0:
  center += 1
  a_len = len(s_aend)-1
else:
  a_len = len(s_aend)

a_center = 0
b_center = 0

for s in s_bstart:
  a_center += s.count("AB")
for s in s_aend:
  b_center += s.count("AB")






print(s_max.count("AB") + center + min(a_len,b_len)+a_center+b_center)

Solution (Python):


N = int(raw_input())
S = list()

_count = 0
_A = 0
_B = 0
_BA = 0
for i in range(N):
    S.append(raw_input())

for s in S:
    if s[-1] == "A":
        _A += 1
    if s[0] == "B":
        _B += 1
    if s[-1] == "A" and s[0] == "B":
        _BA += 1
    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            _count += 1
    
_count += min(_A,_B)
if _A == _B == _BA != 0:
    _count -= 1

print str(_count)

Solution (Python):
n = int(input())
s = []
count = 0
boa = 0
boo = 0
ooa = 0
for _ in range(n):
  i = input()
  s += [i]
  l = len(i)
  for k in range(l-1):
    if i[k:k+2] == 'AB':
      count += 1
  if i[0] == 'B' and i[-1] == 'A':
    boa += 1
  elif i[0] == 'B':
    boo += 1
  elif i[-1] == 'A':
    ooa += 1
if boo==0 and ooa==0 and boa!=0:
  rest = boa-1
else:
  rest = boa + min(boo,ooa)
print(count+rest)

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

count_AB = 0
lefter_B = 0
righter_A = 0
only_lefter_B = 0
only_righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1
    if (S[i][0] == 'B')and(S[i][len(S[i])-1] != 'A'):
        only_lefter_B += 1
    if (S[i][0] != 'B')and(S[i][len(S[i])-1] == 'A'):
        only_righter_A += 1
        
if (only_lefter_B==0) and (only_righter_A==0):
    Appearing_AB = max(min(lefter_B,righter_A) - 1 , 0)
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)

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

count_AB = 0
lefter_B = 0
righter_A = 0
only_lefter_B = 0
only_righter_A = 0
for i in range(N):
    count_AB += S[i].count('AB')
    if S[i][0] == 'B':
        lefter_B += 1
    if S[i][len(S[i])-1] == 'A':
        righter_A += 1
    if (S[i][0] == 'B')and(S[i][len(S[i])-1] != 'A'):
        only_lefter_B += 1
    if (S[i][0] != 'B')and(S[i][len(S[i])-1] == 'A'):
        only_righter_A += 1
        
if (only_lefter_B==0) and (only_righter_A==0):
    Appearing_AB = max(min(lefter_B,righter_A) - 1 , 0)
else:
    Appearing_AB = min(lefter_B,righter_A)


print(count_AB + Appearing_AB)

Solution (Python):
N = int(input())
sum = 0
sum_a = 0
sum_b = 0
sum_ab = 0
for i in range(N):
  s = input()
  sum += s.count('AB')
  if (s[-1] == 'A' and s[0] == 'B'):
    sum_ab += 1
  elif (s[0] == 'B'):
    sum_b += 1
  elif (s[-1] == 'A'):
    sum_a += 1
alpha = 0
if (sum_a == 0 and sum_b == 0 and sum_ab > 0):
  alpha = sum_ab - 1
else:
  alpha = min(sum_a, sum_b) + sum_ab
print(sum + alpha)

Solution (Python):
n=int(input())
s=[]
for i in range(n):
  s.append(input())
cnt_a=0
cnt_b=0
cnt_ab=0
for j in range(n):
  if s[j][0]=='B' and s[j][len(s[j])-1]=='A':
    cnt_ab+=1
  elif s[j][0]=='B':
    cnt_b+=1
  elif s[j][len(s[j])-1]=='A':
    cnt_a+=1
cnt_a+=cnt_ab
cnt_b+=cnt_ab
cnt_2=0
for l in range(n):
  cnt_2+=s[l].count('AB')
if cnt_a==0 and cnt_b==0:
  print(cnt_2)
elif cnt_ab-cnt_a==0 and cnt_ab-cnt_b==0:
  print((cnt_ab+cnt_2)-1)
else:
  print(min(cnt_a,cnt_b)+cnt_2)

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

hd = 0
tl = 0
bt = 0
ans = 0
for s in S:
    for i in range(len(S)-1):
        if s[i:i+2] == 'AB':
            ans += 1
    if s[0] == 'B' and s[-1] == 'A':
        bt += 1
    elif s[0] == 'B':
        hd += 1
    elif s[-1] == 'A':
        tl += 1

if bt == 0:
    ans += min(hd, tl)
else:
    ans += bt - 1
    if hd > 0 and tl > 0:
        ans += 2 + min(hd-1, tl-1)
    elif hd > 0 or tl > 0:
        ans += 1

print(ans)

Solution (Python):
n=int(input())
count=0
dual=0
startb=0
enda=0

f0=0
f1=0
f2=0

for i in range(n):
    tmp=list(input())
    if tmp[0]=="B" and tmp[-1]=="A":
        
        f0+=1
        startb+=1
        enda+=1
    elif tmp[0]=="B":
        startb+=1
        f1+=1
    elif tmp[-1]=="A":
        enda+=1
        f2+=1

    aflag=0
    for t in tmp:
        if aflag==1 and t=="B":
            count+=1
            aflag=0
        elif t=="A":
            aflag=1
        else:
            aflag=0
"""
while dual>0:
    if startb>enda:
        enda+=1
    else:
        startb+=1
    dual-=1
"""
count+=min(startb,enda)


if f1 ==0 and f2==0 and f0>0:
    count-=1
if count<0:
    count=0

print(count)

Solution (Python):
N = int(input())
A = 0
B = 0
BA = 0
ans = 0
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' and S[-1] == 'A':
        BA += 1
    elif S[0] == 'B':
        B += 1
    elif S[-1] == 'A':
        A += 1
    

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

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

Solution (Python):
n=int(input())
ls=[0]*n
for i in range(n):
    ls[i]=input()
numab=0
sA=0
sB=0
sBA=0
for i in range(n):
    sb=""
    for j in ls[i]:
        if sb=="A":
            if j=="B":
                numab+=1
        sb=j
    if ls[i][0]=="B":
        if ls[i][-1]=="A":
            sBA+=1
        else:
            sB+=1
    if ls[i][-1]=="A" and ls[i][0]!="B":
        sA+=1
ans=0
if sA!=0 and sB!=0:
    ans=sBA+1+min(sA-1,sB-1)
elif sA==0 and sB!=0:
    ans=sBA
elif sB==0 and sA!=0:
    ans=sBA
else:
    ans=sBA-1
if ans<0:
    ans=0
ans+=numab

print(ans)

Solution (Python):
N = int(input())
s = [0]*N
num_ab_in = 0
num_hip_a = 0
num_head_b =0 
num_both = 0

for i in range(N):
    s[i] = input()
    slist= list(s[i])
    num_ab_in += s[i].count('AB')
    if slist[0] =='B':
        num_head_b +=1
    if slist[-1] =='A':
        num_hip_a +=1
    if slist[0] =='B' and slist[-1] =='A':
        num_both +=1

if num_both == num_head_b and num_both == num_hip_a and num_both !=0:
    ans = num_ab_in + num_both-1
else:
    ans = num_ab_in + min(num_head_b,num_hip_a)
print(ans)

Solution (Python):
N = int(input())
S = [None] * N
for i in range(N):
    S[i] = input()

ans = 0
endswith_a = 0
startswith_b = 0
both_ab = 0
for s in S:
    ans += s.count('AB')
    a = s.endswith('A')
    b = s.startswith('B')
    endswith_a += 1 if a else 0
    startswith_b += 1 if b else 0
    both_ab += 1 if a and b else 0

if both_ab == endswith_a or both_ab == startswith_b:
    if endswith_a == startswith_b and both_ab > 0:
        ans += both_ab - 1
    else:
        ans += both_ab
else:
    ans += min(endswith_a, startswith_b)

print(ans)

Solution (Python):
N = int(input())
count = 0
ca = 0
cb = 0
cab = 0
for i in range(N):
    si = input()
    for j in range(len(si)-1):
        if si[j] == "A" and si[j+1] == "B":
            count += 1
    if si[-1] == "A" and si[0] == "B":
        cab += 1
    else:
        if si[-1] == "A":
            ca += 1
        if si[0] == "B":
            cb += 1
    

if cab == 0:
    count += min(ca, cb)
elif ca == 0 and cb == 0:
    count += cab - 1
elif (ca == 0 and cb != 0) or (ca != 0 and cb == 0):
    count += cab
else:
    count += cab + min(ca, cb)



print(count)

Solution (Python):
from collections import deque

n = int(input())
a = []
firstB = deque()
lastA = deque()
both = deque()
used = set()
for i in range(n):
    s = input().strip()
    a.append(s)
    if s[0] == 'B' and s[-1] == 'A':
        both.append(i)
        used.add(i)
    elif s[0] == 'B':
        firstB.append(i)
        used.add(i)
    elif s[-1] == 'A':
        lastA.append(i)
        used.add(i)

ret = ''
while both:
    ret += a[both.pop()]
if ret and lastA:
    ret = a[lastA.pop()] + ret
if ret and firstB:
    ret += a[firstB.pop()]
while lastA and firstB:
    ret += a[lastA.pop()] + a[firstB.pop()]
while lastA:
    ret += a[lastA.pop()]
while firstB:
    ret += a[firstB.pop()]
for i in range(n):
    if i not in used:
        ret += a[i]
print(ret.count('AB'))

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]
mae_B = 0
ushiro_A = 0
docchimo = 0
naka_AB = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            naka_AB += 1
    if s[0] == 'B' and s[-1] == 'A':
        docchimo += 1
    elif s[0] == 'B':
        mae_B += 1
    elif s[-1] == 'A':
        ushiro_A += 1
ans = naka_AB
ans += max(0, docchimo-1)
if docchimo > 0:
    if mae_B > 0:
        mae_B -= 1
        ans += 1
    if ushiro_A > 0:
        ushiro_A -= 1
        ans += 1
ans += min(mae_B, ushiro_A)
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = [input() for i in range(n)]

    AB = 0
    B_ = 0
    _A = 0
    B_A = 0
    for ss in s:
        for i in range(len(ss)-1):
            if ss[i:i+2] == 'AB':
                AB += 1
        if ss[0] == 'B':
            if ss[-1] == 'A':
                B_A += 1
            else:
                B_ += 1
        elif ss[-1] == 'A':
            _A += 1
    if B_A > 0:
        if _A > 0 and B_ > 0:
            print(AB + (B_A - 1) + 2 + min(_A - 1, B_ - 1))
        elif _A == 0 and B_ == 0:
            print(AB + (B_A - 1))
        else:
            print(AB + (B_A - 1) + 1)
    else:
        print(AB + min(_A, B_))

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
ans = 0
count_BA = 0
count_A = 0
count_B = 0
for i in range(n):
    s = input()
    ans += s.count("AB")
    if s[0] == "B" and s[-1] == "A":
        count_BA += 1
    elif s[0] == "B":
        count_B += 1
    elif s[-1] == "A":
        count_A += 1

if count_BA > 0:
    ans += count_BA - 1
if count_BA > 0 and count_B > 0:
    ans += 1
    count_B -= 1
if count_BA > 0 and count_A > 0:
    ans += 1
    count_A -= 1
while count_A > 0 and count_B > 0:
    ans += 1
    count_A -= 1
    count_B -= 1

print(ans)